The 17-line secret
Here's the secret that agent frameworks don't want you to know: the core of Claude Code, Cursor, and every AI coding agent is the same loop. Prompt the model. If it returns tool calls, execute them. Feed results back. Repeat until the model has no more tool calls. That's it.
The agent loop: Prompt-tool-result cycle
Every turn: send messages to LLM, get response. If tool calls exist, execute and loop. Otherwise, done.
def agent_loop(messages, tools):
while True:
response = completion(
model=MODEL,
messages=messages,
tools=tools,
)
message = response.choices[0].message
messages.append(message)
if not message.tool_calls:
return # Model is done
for tc in message.tool_calls:
args = json.loads(tc.function.arguments)
result = execute_tool(tc.function.name, args)
messages.append({
"role": "tool",
"tool_call_id": tc.id,
"content": result,
})The entire agent loop. Everything else in this workshop builds on top of this pattern.
Fill in the blanks: Complete the agent loop
Loading practice…
Quiz: Quiz
Loading practice…
Terminal
bash
# Run the first agent
make 01-agent-loop
# Try: "List the files in the current directory"
# Try: "What is 2 + 2?" (it will use bash to calculate)
# Type "exit" to quitRun the first workshop (01-basic-agent) and interact with your first agent. It only has one tool: bash.