A minimalist AI coding agent. ~70 lines of core logic.
A tight loop around three primitives:
- Message history — the agent's memory
- Tool registry — read file, write file, run shell
- LLM call — Claude Sonnet via Anthropic SDK
Built tool call → loop back → until end_turn. That's it.
npm install
npm run build
npm link # makes `ii` available globallyOr run directly:
export ANTHROPIC_API_KEY=sk-ant-...
npm run devii> list the files in src/
ii> read src/agent.ts and explain the loop
ii> add error handling to the bash tool
/clear # reset conversation history
/exit # quit
src/
types.ts — Tool interface
tools.ts — read_file, write_file, bash
agent.ts — the agentic loop (~50 lines)
index.ts — CLI REPL
The agent loop in agent.ts is the whole thing:
- Append user message to history
- Call LLM with full history + tools
- If
stop_reason === "tool_use"→ execute tools in parallel, append results, go to 2 - If
stop_reason === "end_turn"→ return text
import { Agent } from "./src/agent.js";
import type { Tool } from "./src/types.js";
const myTool: Tool<{ query: string }> = {
name: "my_tool",
description: "Does something useful",
inputSchema: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
},
async execute({ query }) {
return `result for ${query}`;
},
};
const agent = new Agent("You are helpful.", [myTool]);
const reply = await agent.prompt("use my_tool with query hello");- Node.js >= 20
ANTHROPIC_API_KEYenvironment variable