# The agent loop and harness

> At its core an agent is a short loop: call the model, run any tool it asks for, feed back the result, repeat until done. This topic builds that loop from scratch, rebuilds it on an agent SDK to see what the SDK takes over, and covers stop conditions and the ways a loop runs away.

Page: https://schubergphilis.github.io/ai-training/topics/building-agents/agent-loop/
License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/

*Building agents* · topic `building-agents/agent-loop`

At its core an agent is a short loop: call the model, run any tool it asks for, feed back the result, repeat until done. This topic builds that loop from scratch, rebuilds it on an agent SDK to see what the SDK takes over, and covers stop conditions and the ways a loop runs away.

## Concepts

- **Loop from scratch**: Writing the agent loop yourself in a few dozen lines: send the conversation and tool definitions to the model, inspect the reply, run any requested tool, append the result, and call again until the model answers without a tool request. Building it once removes the mystery from every agent product and shows where the real difficulty lies: in tools, context and stopping. [glossary](https://schubergphilis.github.io/ai-training/glossary/#loop-from-scratch)
- **Agent SDK**: A library that provides the loop, tool plumbing, context management, permission checks and often built-in tools, so a developer supplies instructions and tools and gets an agent. An SDK saves rebuilding solved parts and encodes hard-won defaults. The cost is that its choices about context, retries and stopping are now yours to understand rather than to write. [glossary](https://schubergphilis.github.io/ai-training/glossary/#agent-sdk)
- **Stop conditions**: The rules that end a run: the model answers without requesting a tool, a maximum number of turns or tokens is reached, a budget is spent, a check passes, or a human stops it. An agent with only the first rule can run indefinitely. Good harnesses combine a natural completion signal with hard limits and report which one fired. [glossary](https://schubergphilis.github.io/ai-training/glossary/#stop-conditions)
- **Unbounded-loop pitfalls**: The ways a loop keeps going wrongly: retrying a failing tool forever, alternating between two fixes, satisfying a check by weakening it, re-reading the same files as context fills, or spending the whole budget on exploration. Each burns money and can cause harm through side effects. Limits, detection of repeated states and visible progress reporting are the defenses. [glossary](https://schubergphilis.github.io/ai-training/glossary/#unbounded-loop-pitfalls)

## Links

- **Builds on:** [Tool use](https://schubergphilis.github.io/ai-training/topics/building-agents/tool-use/)
- **Leads to:** [Evaluation and testing](https://schubergphilis.github.io/ai-training/topics/building-agents/evaluation/), [Design patterns](https://schubergphilis.github.io/ai-training/topics/building-agents/patterns/), [Agentic retrieval and memory](https://schubergphilis.github.io/ai-training/topics/building-agents/retrieval-memory/)
- **Competencies drawing on it:** [Builds a tool-using agent loop](https://schubergphilis.github.io/ai-training/competencies/building-agents/builds-agent-loop/)

## Lessons

- [Building your first agent](https://schubergphilis.github.io/ai-training/building-agents/agent-loop/) (tutorial)
- [Rebuilding the loop on an agent SDK](https://schubergphilis.github.io/ai-training/building-agents/loop-on-an-sdk/) (explanation)
- [Stopping the loop on purpose](https://schubergphilis.github.io/ai-training/building-agents/stopping-the-loop/) (tutorial)

## Sources

- `AEC-13` [Building your first agent: the loop from scratch, then with an SDK](https://lsimons.github.io/agent-engineer-course/13-building-your-first-agent/), Agent Engineer Course (course)
- `Academy claude-platform-101` [Claude Platform 101](https://academy.claude.com/courses/claude-platform-101), Claude Academy (course)
- `Academy building-with-the-claude-api` [Building with the Claude API](https://academy.claude.com/courses/building-with-the-claude-api), Claude Academy (course)
- `OpenAI Academy design-and-build-agentic-systems-k5uke` [Design and Build Agentic Systems](https://academy.openai.com/public/courses/design-and-build-agentic-systems-k5uke), OpenAI Academy (course)
