Plan, call, observe

The agent loop is not magic. The model reads the user message, decides whether a tool would help, emits a tool call, waits for the result, then composes a natural language reply. Plan, call, observe, in that order. Your system prompt is what teaches the model to follow this pattern instead of guessing.

lib/utils.ts
typescript
export const SYSTEM_PROMPT = `You are a helpful AI assistant that can use tools to answer questions.

When the user asks a question:
1. Decide whether a tool call would help answer it.
2. If yes, call the tool with the right arguments.
3. Observe the tool result before you reply.
4. Respond in plain English using the tool result as grounding.

Do not invent data the tool did not return. If no tool fits the question, answer from general knowledge.`;

The system prompt names the loop explicitly. Models follow instructions better when the pattern is spelled out in numbered steps.

Plan, call, observe

One user turn can involve several plan-call-observe cycles. Today we keep it to one cycle.

The first response is not text. The model returns a tool_call message containing the tool name (get_weather) and a JSON arguments object ({ city: "Tokyo" }). The SDK intercepts that, runs execute on your server, and appends a tool_result message to the conversation. The model then produces a natural language reply on its next turn.

Quiz: Quiz

Loading practice…

Checkpoint: Foundations checkpoint

Loading practice…