Building a Terminal Agent (Nanocode)
Terminal agent loop: think, act, observe, repeat
Terminal agent = model + tools + loop. Claude Code, Codex and OpenCode are this, only optimised and more robust.
- Think - model decides: call a tool or give the final answer
- Act - choose an action, run the tool
- Observe - tool output goes back to the model
- Repeat - until the model has enough evidence
Each response ends for one of two reasons, read from the metadata the API returns.
- Tool call - run it and loop again
- Stop - model has the final answer, so finish
Turn - one model-to-assistant step. "This agent took 20 turns" means 20 such steps.
Designing a tool
- Name - e.g. read_file
- Description - what it does
- Parameters - e.g. file name, optional line range 10 to 20
- read_only boolean - safe to run without asking permission?
- Execute - the real code that runs with the args
Name + description act like an API the model searches. It matches its need to a tool, then fills the parameters.
The model never sees the tool code. Only name, description, parameters. A vague description means the model won't pick the tool.
- Tools are model agnostic. A tool built for one agent works with any model.
- Flow: model names tool + args -> system checks read_only -> execute runs -> output returns to model
Permissions and skip mode
- read_only = true -> run directly
- Can write or is irreversible -> pause, user approves or denies
- Dangerously skip / autonomous mode -> ignore the flag
Statelessness, system prompt and compaction
Model and API are stateless. Append every message, tool call and output to the transcript and resend it. Caching keeps cost down.
System prompt - a text blob placed before any user message.
- Persona: you are a coding agent, be concise
- Tool definitions
- CLAUDE.md rules - just appended text, no learning
- Project-specific memory
Compaction - summarising the session. Two types:
- Micro compaction - harness drops very big or old tool outputs and images; model can call the tool again if needed
- Full compaction - summarise everything when the context window fills
Extras: MCP, hooks, tool search, plan mode, sub-agents
- MCP - reach apps such as Email or Slack through hosted servers when no API is available
- Hooks - commands the harness runs at specific events
- Tool search - tool the model uses to find the right one among hundreds, instead of listing all
- Plan mode - no writes allowed; the goal is a plan
- Sub-agent - an agent inside an agent
Sub-agent is just another tool: name e.g. task, parameters like a prompt. Calling it starts a fresh agent loop with the given context. Only the final answer returns to the main agent, not its context.
Beyond this, what remains is optimisation.
Building Nanocode step by step
Approach: give the coding agent a basic brief, then one step at a time. Test each step.
| Tool | Parameters | Read only |
|---|---|---|
| read_file | path (required) | Yes |
| write_file | path, content (creates file if missing) | No |
| edit_file | path, exact string to replace, replacement | No |
| grep | regex pattern (ripgrep is faster) | Yes |
| bash | command | No, behind permission gate |
- Test: ask the agent to read nanocode.py and summarise it
- Test permissions: approve or deny prompts appear for non-read-only tools
- nanocode.md is appended to the system prompt, like CLAUDE.md
- Todo tool: list tasks and mark in-progress or complete
- Plan mode: tools disabled, toggled by a slash command
Review generated code. A web tool asked for an exact URL, which is a poor design. Agents work better when they give search context and the search API (Exa/Firecrawl) finds pages.
Extending Nanocode: self-improvement and challenge
- Ask Nanocode to improve its own code: docstrings, dataclasses, separated concerns, cached tool instances
- Add: visible tool calls, model reasoning display, split into rules.py and utils.py
- Challenge: maximise the score on Terminal-Bench 2.1 with the same model
- Learn from open-source harnesses (pi, Codex)
- Ideas: recursive self-improving agent, RLM, auto-research loop
Glossary
- Turn
- One model-to-assistant step in the agent loop.
- Compaction
- Summarising the session to free context window space.
- Micro compaction
- Dropping big, old tool outputs or images from context.
- Sub-agent
- An agent spawned by a tool inside another agent; only its final answer returns.
- Hooks
- Commands the harness runs at specific events.
- Plan mode
- Mode where writes are disallowed and the agent produces a plan.