# How Do Coding Agents Work?

> How a coding agent reads your code, edits files, runs tests and proposes a change, and where it can go wrong, explained for your AI interview.

**In short:** A coding agent is a language model in a loop, with tools. It reads your code, edits files and runs the tests, repeating until the checks pass or a limit stops it. A person reviews the change before it ships.

AI and LLM interview question Q189, Agents and tool use. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-coding-agents-work

## Say it in 30 seconds

A coding agent is an AI model that works on your code. You give it a task, and it reads files, makes an edit, runs the tests and reads the output. Then it repeats those steps until the checks pass. A person still reviews the change before it goes live. The loop is simple. What matters is which tools it can use, and the checks that stop it doing damage.

## Say it in 2 minutes

A coding agent is a language model with a set of tools and a loop. You give it a task, such as a bug report. The model reads the task, picks one tool, like searching the code or opening a file, and your side runs it. The result goes back to the model, which decides the next step. Anthropic's December 2024 guide describes agents as LLMs using tools based on feedback in a loop, and that is the whole core.

What makes coding a good fit is that code has a clear check. Tests either pass or fail, so the agent gets a clear signal about its own work. The practical trick is to give it the test command up front, so it runs the checks after every edit and reads each failure.

There are risks. Guard against an agent that changes a test so it passes instead of fixing the bug, or that edits a file outside the task. So its tools are limited by permission rules and a sandbox, which restricts the folders and servers it can reach. Anthropic says sandboxing reduces permission prompts by 84% in its own internal use.

In an interview, cover three things: the loop, the checks, and the limits. Then say a person reviews the pull request before anything ships. That last step is how real teams keep the risk small.

## What is a coding agent, in plain words?

A coding agent is an AI model that works inside your codebase, the folder of code a project lives in. You give it a task, such as fixing a bug or adding a field to an API. It reads the code, edits files, runs commands and checks its own work. Then it reports back.

Anthropic's December 2024 guide describes the idea in one line: agents are "typically just LLMs using tools based on environmental feedback in a loop". Here LLM means large language model. Three words carry it: tools, feedback and loop.

Think of a new engineer in their first week. They get a ticket, read the code around it, make a change, run the tests and send it to a senior colleague to review. A coding agent follows the same routine, much faster. It sees only what it is shown, so the setup around it matters.

A real case: a payments team gets the ticket "Refunds on split-card orders come out one cent short." Orders paid with two cards get their refund rounded on each card separately. The agent finds the rounding code, fixes it, runs the 412 tests and opens a pull request. A developer reads the diff and merges it.

- **Codebase:** All the code, config and tests for one project, usually kept in a repository (repo).
- **Tool:** A function the model can ask for, such as read a file, search, edit or run tests. Your code runs it.
- **Test suite:** The automated checks that confirm the code still does its job. Each one passes or fails.
- **Pull request:** A proposed change that a person reviews before it is merged into the main code.
- **Sandbox:** A fenced-off area where the agent can only touch approved files and servers.

## How does the loop work, step by step?

The model never touches your files itself. It only asks. Each turn it reads the task and everything so far, then picks one tool with its inputs. Your side runs the tool and sends the result back. The next turn starts from that result.

*Diagram, One coding agent, one loop: The model asks for a tool, the tool runs in your project, and its result goes back to the model. The loop stops when the checks pass and the change is ready for review, or when a stop rule trips.*
1. Your task starts the loop.
2. The model asks for a tool, such as a file read or a test run.
3. The tool's result goes back to the model, which picks the next step.
4. When the checks pass, the change goes to a person as a pull request.

1. **Read the task:** The model gets the ticket and the project's instructions file, then asks for the files it needs.
2. **Search and read:** It searches for the rounding code and opens only those files. It can hold just so much text at once, which is its context window.
3. **Edit:** It proposes a change to the code. You see the change as a diff, the list of lines added and removed.
4. **Run the checks:** It runs the tests, reads the failures and goes round again if any of them fail.
5. **Stop:** It stops when the checks pass, or when a limit is reached, such as a maximum number of steps.

> "During execution, it's crucial for the agents to gain "ground truth" from the environment at each step (such as tool call results or code execution) to assess its progress." (Anthropic, on agents)
>
> "The task often terminates upon completion, but it's also common to include stopping conditions (such as a maximum number of iterations) to maintain control." (Anthropic, on stopping)

> **Tip:** Name the stop rule in your answer. An agent with no limit can keep retrying the same failing step, so a limit on the number of steps and a spending budget belong in the design.

The run tool is what makes the loop work. Without it, the model has to guess whether its code compiles or its tests pass.

## Why do tests decide whether the work is done?

A coding agent can be wrong and still sound sure. Code suits agents well because a test either passes or fails. Anthropic's December 2024 guide says code suits agents for this reason: "Code solutions are verifiable through automated tests", and agents "can iterate on solutions using test results as feedback".

- **Good fix: The bug is fixed.** The agent finds that the rounding runs on each card. It fixes the total per order instead. All 412 tests pass, and a new test for split orders passes too.
- **Bad fix: The test is changed.** The agent edits the failing test so it expects the wrong total. Everything goes green and the bug ships. A reviewer who reads the diff would catch it.

Tests check behaviour. They cannot tell whether a change fits the wider system, so a person still reviews it.

## How do you keep a coding agent safe?

An agent that can run commands needs limits. In its sandboxing post, Anthropic said Claude Code is read-only by default. It asks before it changes a file or runs a command, with a few exceptions: it auto-allows safe commands such as echo or cat. The same post explains why asking every time is not enough on its own.

Constant approval has a cost. Anthropic warns of approval fatigue, where people may not pay close attention to what they approve. Its answer is a sandbox: fixed boundaries the agent works inside without asking each time. It has two parts, a filesystem limit and a network limit.

*Diagram, Checks between the model and your files: The model's step must pass both checks before it runs: your permission rules and the sandbox. A step the rules deny, or one outside the project, is blocked. The numbered arrows show the path.*
1. The model's step goes through the checks first.
2. A step the permission rules deny is blocked.
3. A step outside the project is blocked by the sandbox.
4. A step that passes both checks runs.

The filesystem part lets the agent read and write in the project folder, and blocks changes to files outside it. The network part lets it reach only approved servers. Without the network part, a compromised agent could send out sensitive files. Anthropic says sandboxing reduces permission prompts by 84% in its own internal use.

Here is the gate in plain Python. The model is a fixed script, and the code decides what gets through.

```python
# A simplified coding-agent loop. The "model" is a fixed script, so the run is repeatable.
import posixpath
PROJECT_FILES = ("refunds.py", "tests/")

def allowed(path):
    # Code decides what a tool may touch, not the model.
    return posixpath.normpath(path).startswith(PROJECT_FILES)

def run_tool(name, arg, state):
    if name == "edit" and not allowed(arg):
        return "BLOCKED: " + arg + " is outside the project"
    if name == "edit":
        state["edited"] = True
        return "edited " + arg
    if name == "test":
        return "tests " + ("passed" if state["edited"] else "failed")
    return "read " + arg

script = [("read", "refunds.py"), ("edit", "refunds.py"),
          ("test", "tests/"), ("edit", "/etc/hosts")]

state = {"edited": False}
history = []
for turn, (name, arg) in enumerate(script, start=1):
    result = run_tool(name, arg, state)
    history.append(result)  # the result goes back to the model
    print(turn, result)
print("done after", len(history), "steps")
```

Output:

```text
1 read refunds.py
2 edited refunds.py
3 tests passed
4 BLOCKED: /etc/hosts is outside the project
done after 4 steps
```

## Where do coding agents go wrong?

- **Changing the test, not the code:** It edits a failing test until it passes. Check the diff for any test that changed.
- **Editing outside the task:** It fixes a config file it happened to notice. Limit the tools and scope each task.
- **Missing the file that matters:** On a large repo, the file that holds the bug may never get opened. Give it the entry points and let it search.
- **Fixing the symptom:** The error goes away, but the cause is still there. Read the failing case, not just the green result.
- **Running out of room:** It hits a step limit or the context limit before the work is done. Split the task into smaller ones.

Each of these has the same safety net: a person reads the diff before it merges.

### How would you roll a coding agent out to a 40-person team?

Start small and make each step visible. The aim is to make the agent's mistakes cheap and easy to spot.

1. **Pilot on one service:** Pick a service with good tests and a clear owner. Give the agent read-only access first, so it can look but not change anything.
2. **Set the limits:** Add a step limit, a spend limit per task and a stop rule before the first run.
3. **Keep the checks:** Every pull request runs the same automated checks on a server (CI), whoever or whatever wrote it.
4. **Log every tool call:** Keep a record of what the agent read, edited and ran, so a bad run can be traced.
5. **Judge by outcomes:** Count changes that merge cleanly and rollbacks, not lines written.

Widen the access only after a few weeks of clean runs, and keep a person on every merge.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a language model in a loop, with tools and feedback.
- [ ] The loop: the model picks a tool, the tool runs, the result goes back.
- [ ] Why tests matter: a clear pass or fail gives the agent a signal.
- [ ] The risks: edits outside the task, weakened tests and unchecked commands.
- [ ] Safety: permission rules, a sandbox and a human reviewing the diff.
- [ ] Honest limits: it can be wrong and still sound sure.

**Common traps**

- Trap: "The agent writes perfect code, so tests are optional." Better: Tests are what let it check itself. Without them it has to guess.
- Trap: "It runs commands on its own, with no limits." Better: It works inside permission rules and a sandbox, and a person reviews the change.
- Trap: "Coding agents are just autocomplete." Better: Autocomplete suggests the next line. An agent picks tools, runs them and acts on the results over many steps.

## Can you spot the risk?

**Spot the risk:** Which agent setup is the riskiest?

A. It runs tests in a sandbox, opens a pull request, and a developer reviews the diff.
B. It has write access to the whole monorepo and the production secrets file, and runs every command without approval.
C. It has write access to the monorepo, but it asks before each command and a developer reviews the diff.
D. It works on a branch, stops after 40 steps and logs every tool call to the ticket.

**Answer:** B. Write access to everything, live secrets and no approvals together mean one wrong step can reach production. Limit the access and keep a person on the risky steps.

**Quick check:** A coding agent's tests fail after its edit. What should it do next?

A. Commit the edit and open a pull request, since a person will review it
B. Read the failure output, change the code, and run the tests again
C. Delete the failing test
D. Run the same tests again with no changes, in case the failure was random

**Answer:** B. The test output is the feedback the loop needs. Shipping the edit, deleting the test or rerunning unchanged code leaves the bug in place.

## Can you answer these from memory?

**Flash cards**

- **What is a coding agent?** A language model in a loop, using tools like read, edit and run tests on a codebase.
- **What does the loop do?** The model picks a tool, the tool runs, the result goes back, and it repeats until done.
- **Why do tests help an agent?** A clear pass or fail gives it feedback to check its own work.
- **What is the sandbox for?** It limits which files and servers the agent can touch, so fewer prompts are needed.
- **What is the main risk?** Changing tests to fit broken code, or editing files outside the task.
- **Who has the last word?** A person reviews the diff before the change is merged.

## Likely follow-up questions

- Q160 What is the agent loop? https://designervenkat.online/interview/ai/what-is-the-agent-loop
- Q176 How should an agent handle tool errors and retries? https://designervenkat.online/interview/ai/how-should-an-agent-handle-tool-errors-and-retries
- Q180 How do you sandbox code-running agents? https://designervenkat.online/interview/ai/how-do-you-sandbox-code-running-agents
- Q173 How do you stop endless agent loops? https://designervenkat.online/interview/ai/how-do-you-stop-endless-agent-loops
- Q190 How do you trace and debug an agent run? https://designervenkat.online/interview/ai/how-do-you-trace-and-debug-an-agent-run
- Q175 What makes a tool-calling loop reliable enough to ship? https://designervenkat.online/interview/ai/reliable-tool-calling-loop

## Learn it in depth

- [AI coding tools](https://designervenkat.online/learn/ai-engineering/ai-coding-tools)
