← govind p lal
AI and ML Bootcamp · Lecture 28 · lecture notes

Building a Terminal Agent (Nanocode)

In short — How does a terminal coding agent like Claude Code or Codex work, and how do you build a small one? A terminal agent is the same model + tools + loop as any agent, with coding tools, a system prompt, permissions and a few extras on top. The lecture covers the architecture, then builds "Nanocode" step by step.

Terminal agent loop: think, act, observe, repeat

adds to earlier · Four-step loop naming, stop reasons, and the definition of a turn in a terminal agent. · builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

Terminal agent = model + tools + loop. Claude Code, Codex and OpenCode are this, only optimised and more robust.

needs toolno tool neededobserve outputUser requestModelThinkRun toolActFinal answer
The agent loop.

Each response ends for one of two reasons, read from the metadata the API returns.

Turn - one model-to-assistant step. "This agent took 20 turns" means 20 such steps.

Designing a tool

adds to earlier · Tool contract fields including read_only flag, model-agnostic tools, and the model never seeing code. · builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

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.

Permissions and skip mode

builds on Lecture 18: Week 17 - AI&ML | Harness, Context, and Evals | 09/05/2026

Statelessness, system prompt and compaction

adds to earlier · System prompt contents, CLAUDE.md as appended text, micro vs full compaction. · builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 18: Week 17 - AI&ML | Harness, Context, and Evals | 09/05/2026

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.

Compaction - summarising the session. Two types:

Extras: MCP, hooks, tool search, plan mode, sub-agents

adds to earlier · Hooks, tool search, plan mode, and sub-agent as a plain tool returning only final answer. · builds on Lecture 18: Week 17 - AI&ML | Harness, Context, and Evals | 09/05/2026, Lecture 21: Week 21 - AI&ML | LangGraph | 06/06/2026

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

builds on Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026, Lecture 9: Week 8 - AI&ML | From APIs to Agents | 07/03/2026

Approach: give the coding agent a basic brief, then one step at a time. Test each step.

Chat loop viaOpenRouterTool base classread_file in theloopwrite, edit, grep,bashPermission gatenanocode.md inpromptTodos + sub-agentWeb tool, plan modeStreaming + CLI
Build order.
ToolParametersRead only
read_filepath (required)Yes
write_filepath, content (creates file if missing)No
edit_filepath, exact string to replace, replacementNo
grepregex pattern (ripgrep is faster)Yes
bashcommandNo, behind permission gate
!

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

builds on Lecture 17: Week 16 - AI&ML | Offline: RL Environments for LLMs | 08/05/2026, Lecture 12: Week 11 - AI&ML | Recursive Language Model | 28/03/2026

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.