Code: github.com/muaazdev/agent-50-lines
Every "build an agent in 50 lines" tutorial ends the same way: it prints a nice answer and stops. That's the easy 20%. I wrote a real one, ran it against a real repository, and kept notes on everything that went sideways. This post is those notes.
The agent answers questions about a codebase. You point it at a folder, ask "where is --help implemented?", and it explores with three tools: list_files, read_file, grep. No framework. Just the Anthropic SDK and a loop. 49 non-blank lines, including imports.
The loop is the easy part
An agent is, in Anthropic's words, a system where "LLMs dynamically direct their own processes and tool usage". Mechanically, that's this (simplified; the repo has the full version):
for step in range(1, MAX_STEPS + 1):
resp = client.messages.create(model=MODEL, max_tokens=1500, tools=TOOLS, messages=messages, system=...)
messages.append({"role": "assistant", "content": resp.content})
if resp.stop_reason != "tool_use": break # model is done
results = [run_tool(b) for b in resp.content if b.type == "tool_use"]
messages.append({"role": "user", "content": results})
Call the model. If it asks for tools, run them and send the results back as a user message. If it doesn't, you're done. That's it. Anthropic's own guidance is to "start by using LLM APIs directly: many patterns can be implemented in a few lines of code." They're right, and it's why I skipped frameworks here. If you can't write this loop, a framework will hide the one thing you need to understand when something breaks.
The other ~35 lines are what turn the toy into something you can run on a real repo.
What the other 35 lines are for
1. A step cap. MAX_STEPS = 12. Without it, a confused model loops until your budget does. Anthropic's guidance also lists a maximum number of iterations as a common stopping condition. I use a for ... else so hitting the cap is loud: it prints !! stopped: hit MAX_STEPS.
2. Errors go back to the model, not to your terminal. Every tool call is wrapped, and failures come back as a tool_result with is_error: true. This turned out to matter in practice. On my second test question the model called read_file with an argument I never defined (host). On the first one it passed a start_line I don't support either. Those are Python TypeErrors. A crash would have ended the run. Instead the error text went back, and the model carried on without them.
3. A path sandbox. safe() resolves every path and refuses anything outside the folder you passed in. I tested it directly: I asked the agent to read /etc/passwd. The tool returned "path escapes the sandbox", and the model reported that it couldn't do it and offered to look inside the project instead. If you give an agent a file tool and no boundary, you've built a file-exfiltration tool with a chat interface.
4. Output truncation. One huge file shouldn't be able to flood the context window, so read_file caps at 8000 characters. This one bit me, and I'll come back to it.
What happened when I ran it on real code
I pointed it at pallets/click, a real, mid-sized Python library. Model: claude-sonnet-5-5. These are my actual runs:
| Question | Steps | Input tokens | Output tokens |
|---|---|---|---|
Where is --help implemented, and how does it exit? | 7 | 63,434 | 1,489 |
| How does click decide whether to show colors? | 7 | 57,586 | 1,576 |
Read /etc/passwd (sandbox test) | 2 | 1,268 | 162 |
At the list price on Anthropic's models page ($2 / $10 per million input / output tokens for Sonnet 5.5), the first two runs work out to roughly 13 to 14 cents each. That's my arithmetic from the published price, not a figure from the API response. Treat it as an estimate.
The answers were good. The first one found help_option() in decorators.py, the eager flag setup, and traced the exit through ctx.exit() to raise Exit(code) to sys.exit() in main(), with file paths and line numbers. It also said which parts it hadn't read directly. I didn't expect that, and I wouldn't count on it.
What nobody tells you
Cost scales with steps, not with answer length. The first run produced about 1,500 output tokens and consumed 63,000 input tokens. Each step re-sends the entire history, including every file the agent has read so far. A "simple" question that takes 7 steps costs far more than seven times a single call would. The step count is the cost driver, so watch it.
Your tool limits become the model's problem. I truncated read_file at 8000 characters and gave it no offset. For long files the model couldn't read past the cutoff, so it improvised. In the first run it fell back to regex greps like ^\s+(\S.*)?$ to pull code out in pieces. It worked, but it's a clumsy way to read a file, and it burned steps. The fix is a start parameter on read_file. I left it out on purpose so you can see the symptom. Tool design is real engineering. Anthropic says to put as much effort into your agent-computer interface as you would into a human-facing UI, and I'd agree.
Tool descriptions are the prompt. The model decides which tool to call and how from the name, description and schema. Anthropic's tool docs recommend several sentences per description and call it the most important factor in tool performance. Mine are one line each, which is probably why the model invented arguments.
The model will tell you what's wrong with your agent. I asked the finished agent how it handles errors and stop conditions. It read its own source and listed three real gaps:
- The API call itself has no error handling, so a network failure crashes the run.
- There's no token or cost budget, only a step cap.
- If the step cap hits, the last turn is a tool request, so there's no final "wrap up" answer.
All three are true, and I haven't fixed them. I haven't hit the step cap in a live run either, so I can't tell you how that path behaves in practice. That's a good list of what separates 49 lines from production.
What 49 lines doesn't give you
This is a demo with a real loop, not a system you'd put in front of customers. Missing: retries and backoff, cost budgets, prompt caching so you stop paying for the same history every step, observability (which tool call went wrong, at which step), evals to tell you whether a change made it better or worse, and anything resembling permissions beyond a folder boundary.
That list is the reason frameworks exist. The Anthropic SDK has a Tool Runner that runs this loop for you, and there are agent SDKs and graph-based frameworks like LangGraph for state, persistence and human-in-the-loop steps. My rule of thumb: write the raw loop once so you know what's underneath, then adopt a framework when you hit a need it solves. Anthropic's advice matches: if you use a framework, make sure you understand the underlying code.
Try it
git clone https://github.com/muaazdev/agent-50-lines
cd agent-50-lines && pip install -r requirements.txt
export ANTHROPIC_API_KEY=sk-ant-...
python agent.py /path/to/any/repo "Where is authentication handled?"
Then break it on purpose. Ask it something that needs a big file. Give it a path outside the sandbox. Lower MAX_STEPS to 2. What you learn from watching it fail is worth more than the 49 lines.
Next in the series: why your RAG pipeline hallucinates, and how to fix it.
Sources
- Anthropic, Building effective agents: the agent definition, "start by using LLM APIs directly", stopping conditions, agent-computer interface design.
- Anthropic, Define tools: tool definition fields and the guidance on detailed descriptions.
- Anthropic, Models overview:
claude-sonnet-5-5model ID and per-token pricing used for the cost estimate. - pallets/click: the repository used for the test runs. Raw output is in
sample-runs.txtin the repo.