TinyCode

A simplified MVP that shows how Claude Code works under the hood — agentic loop, tool use, streaming, all in ~300 lines of TypeScript.

pnpm tsx src/cli.ts claude-sonnet-4-6 5 tools

How It Works

The agentic loop — a simple while loop that calls Claude, executes tools, and repeats.

User types prompt │ ▼ ┌─────────────────────────────────────────────────────┐ │ client.messages.stream() │ │ Send: system prompt + conversation + tool defs │ │ Stream: text deltas appear in terminal in realtime │ └─────────────────────┬───────────────────────────────┘ │ stop_reason? ╱ ╲ "end_turn" "tool_use" │ │ Return to Execute tool locally REPL ┌──────────────────────┐ │ read_file → fs.readFile │ │ write_file → fs.writeFile │ │ edit_file → string replace │ │ list_files → glob │ │ run_command→ child_process │ └──────────┬───────────┘ │ Append tool_result to conversation history │ ▼ Loop back to stream() max 20 iterations

Five Core Tools

The minimum set needed for an agentic coding assistant.

read_file

Read file contents with line numbers. Truncates at 50KB to stay within context budget.

write_file

Create or overwrite files. Auto-creates parent directories if they don't exist.

edit_file

Precise string replacement. Finds an exact match and replaces it — fails if not unique.

list_files

Glob-based file discovery. Ignores node_modules and .git. Returns up to 200 matches.

run_command

Execute shell commands with 30s timeout. Returns stdout, stderr, and exit code.

API Calls Per Session

Each agentic turn = 1 API call. Complexity determines how many turns a task needs.

Session Type API Calls Input Tokens Output Tokens Est. Cost
Simple Q&A 1 200 – 500 300 – 1.5K $0.001 – $0.005
Read & Explain File 2 – 3 2K – 8K 1K – 4K $0.01 – $0.05
Single File Edit 3 – 5 5K – 15K 2K – 8K $0.03 – $0.10
Multi-File Refactor 10 – 30 20K – 80K 10K – 40K $0.15 – $0.60
Build New Feature 15 – 50+ 50K – 200K 20K – 100K $0.50 – $3.00

Context grows with each turn because the full message history is sent every time. By turn 20, input tokens can be 5–10x the initial prompt.

Latency Profile

Streaming makes the experience feel responsive even when total generation time is several seconds.

300-800ms
First token (streaming)
10-500ms
Tool execution (local)
1-5s
Full agentic turn
<1s
User-perceived latency
Tool Typical Latency Notes
read_file 5 – 50ms Filesystem read, limited to 50KB
write_file 5 – 50ms Filesystem write + mkdir
edit_file 5 – 100ms Read + string replace + write
list_files 10 – 200ms Glob traversal, varies with directory size
run_command 50ms – 30s Depends on command; timeout at 30s

Interactive Demos

Simulated sessions showing TinyCode in action. Watch the agentic loop unfold step by step.

tinycode

Source Architecture

~300 lines of TypeScript, 6 files, zero abstractions.

File Purpose Key Function
src/cli.ts REPL entry point readline loop, command dispatch
src/agent.ts Agentic loop engine runAgentTurn() — stream + tool dispatch
src/render.ts Terminal output ANSI colors, streaming text, tool call display
src/tools/index.ts Tool registry Definitions + executeTool() dispatch
src/tools/*.ts Tool implementations 5 files, one per tool