# The agent loop

Call the model with tools. If it asks for tools, run them, append the results and call again. If it answers in text, stop.

AI developers · Part 04: Agents · Card 2 of 11 · Last updated 2026-10-13

## Analogy

A polling loop that ends when the response says "done" instead of "keep going".

## Code example

`agent.ts`

```typescript
async function runAgent(goal: string) {
  const messages = [
    { role: "system", content: "You are a dev-team assistant. Use tools." },
    { role: "user", content: goal },
  ];

  for (let step = 0; step < MAX_STEPS; step++) {
    const res = await client.chat.completions.create({ model, messages, tools });
    const message = res.choices[0].message;
    messages.push(message);

    if (!message.tool_calls) return message.content; // done

    for (const call of message.tool_calls) {
      const result = await runTool(call);
      messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(result) });
    }
  }
  return "Stopped: step limit reached";
}
```

## In an agent

Every agent framework runs this loop under the hood. Know it cold and the frameworks stop being magic.

## Practice

### Flashcards

- **What is an agent?** An LLM in a loop with tools that decides its own next step.
- **Interview: what is an AI agent?** An LLM in a loop with tools. It picks the next tool at runtime from the results so far, until the goal is met.

### Interview questions

**What is an AI agent?**

- An LLM running in a loop with access to tools.
- It decides at runtime which tool to call, looks at the result, and continues until the goal is met.
- Example: DevPilot lists open PRs on its own, then fetches reviews only for the stale ones.

## Questions people ask

### How does the agent loop work?

Call the model with the messages and the tools. If the reply asks for tools, run each one, append the assistant message and the tool results, and call the model again. If the reply is plain text, that is the answer, so stop. Every agent framework runs this same loop underneath.

### What does the agent loop look like in code?

A for loop that counts steps up to a maximum. Inside, call chat.completions.create with messages and tools. If the message has no tool_calls, return its content. Otherwise push the message, run the tools, push one tool message per result, and go round again. After the loop, return a step limit message.

## Related concepts

- [Agent](https://studyvisually.com/learn/tech/ai-developers/agents/agent)
- [The model plans the steps](https://studyvisually.com/learn/tech/ai-developers/agents/model-plans-the-steps)
- [Stop conditions](https://studyvisually.com/learn/tech/ai-developers/agents/stop-conditions)
- [Agent vs workflow](https://studyvisually.com/learn/tech/ai-developers/agents/agent-vs-workflow)

Web page: https://studyvisually.com/learn/tech/ai-developers/agents/agent-loop
Study this card in the app: https://studyvisually.com/app/learn?category=ai-developers&card=agent-loop
