News / Learn / AI coding tools
How do AI coding agents work?
You'll be able to explain the loop an AI coding agent runs and where its mistakes come from.
In plain English
A coding agent is a language model that can take actions. It reads your code, runs commands, edits files, and checks the results. It repeats this cycle until a task is done or it gets stuck. The model does not run code itself. Your tools do, and the model reads what they return.
Why it matters
Most surprises from coding agents come from the loop, not the model's raw skill. If you know how the loop works, you can predict when an agent will fail, what it can see, and what you need to give it. That knowledge helps you design safer products and set realistic expectations for users.
How it works
At the core is a large language model. It takes text as input and produces text as output. The input is called the context window. It holds your request, the project instructions, the files the agent has read, and the results of earlier actions. The model has a fixed size limit for this window, measured in tokens. Anything outside the window is invisible to the model.
An agent wraps the model in a loop. The harness sends the context to the model. The model replies with either a final answer or a tool call, such as 'read this file', 'run this shell command', or 'replace this text'. The harness runs the tool, adds the result to the context, and sends everything back to the model. This cycle is often called the agent loop.
Tool calling works through structured output. The model writes a request in a fixed format, usually JSON, that names the tool and its arguments. The harness checks that format before running anything. If the format is wrong, the call fails and the model usually tries again.
The model does not remember past sessions by default. Continuity comes from the context you pass in, from files the agent writes, and from instruction files the harness loads at the start. Some tools add search over your codebase, using embeddings, so the agent can pull in relevant code without reading everything.
Each step adds to the context. Long sessions fill the window, and older details can get dropped or blurred. Tool output can be large, such as test logs, so it crowds out other information.
A concrete example
A simplified agent loop in Python. The model call is a stub, but the structure matches real harnesses.
def run_agent(task, call_model, tools, max_steps=10):
messages = [{"role": "user", "content": task}]
for _ in range(max_steps):
reply = call_model(messages) # returns text or a tool request
if reply["type"] == "final":
return reply["content"]
name, args = reply["tool"], reply["args"]
if name not in tools:
result = f"error: unknown tool {name}"
else:
result = tools[name](**args)
messages.append({"role": "assistant", "content": str(reply)})
messages.append({"role": "tool", "content": str(result)})
return "stopped: step limit reached"A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Assuming the agent knows what it has not read The model only sees what is in its context. Point it to the files that matter, or make sure your tool can search the codebase.
- Trusting the final message without checking actions Review the tool calls and their results. A confident summary can hide a failed command or an edit that never happened.
- Letting sessions run very long Long sessions crowd the context window. Start a fresh session for new tasks and carry key facts forward in a file.
- Expecting memory across sessions Store durable project rules in an instruction file the harness loads. Do not rely on the model recalling last week's conversation.
Going deeper
Learn how tool schemas are written, how harnesses handle permissions and sandboxing, and how context is compacted or summarized when a session grows long.
FAQ
Does the model run my code?
No. The harness runs commands and tools on your machine or in a sandbox. The model reads the output and decides what to do next.
Why does the agent sometimes ignore my instructions?
Instructions compete with everything else in the context window. Keep them short and specific, and place the most important rules where they are loaded every session.
What is a token?
A token is a small chunk of text, often part of a word. Context window sizes and usage are both measured in tokens.
Can an agent break my project?
Yes. It can delete files or run harmful commands if it has permission. Limit permissions, use version control, and review changes before merging.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.