Inside Claude Code: an AI agent is really a loop around a model
TL;DR
On its own, a language model only produces text. An AI coding agent is a harness around the model: it provides tools (read/write files, run commands), runs a think → act → observe loop, manages context, asks for permission and keeps logs. Knowing this structure helps you delegate to and control agents.
Layers of a harness
| Layer | What it does |
|---|---|
| Tool use | The model returns a tool request (read a file, run shell); the harness executes it and sends back the result. |
| Agent loop | Repeat think → act → observe until the model says it’s done. |
| Safety | Ask before dangerous commands, sandboxing, directory limits, logging. |
| Context management | Prompt caching, compacting history near the limit, delegating side work to sub-agents. |
| Extensibility | Skills, MCP and hooks add capabilities without changing the core. |
A minimal loop
messages = [user_task]
while True:
reply = model(messages, tools=TOOLS)
if not reply.tool_calls:
break # model says it's done
for call in reply.tool_calls:
if needs_approval(call): ask_user(call)
result = run_tool(call) # read files, run commands…
messages.append(result) # observeHow to put it to work
- Give the agent a way to verify itself: test, lint and build commands — the loop is only as good as the right/wrong signal it observes.
- Context is a budget: long logs and command output fill it up; keep tasks tight and split sessions.
- Permissions: knowing the harness executes commands on your behalf helps you configure sensible permissions instead of “allow everything”.
- Go deeper: building a mini agent with the tool-use API is the fastest way to learn.