Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Overview

Welcome to Building a Coding Agent in Rust -- a hands-on tutorial where you build your own AI coding agent from scratch in the mini-claw-code-starter template, guided by the architecture of Claude Code.

Looking for the original V1 hands-on tutorial? It's archived at archive/v1-book/en/ (Chinese translation at archive/v1-book/zh/).

What you'll build

By the end of this book, you'll have built a complete coding agent that:

  • Connects to an LLM via an OpenAI-compatible HTTP provider
  • Uses tools -- bash, file read/write/edit -- with a simple Tool trait
  • Loops autonomously -- the SimpleAgent drives the provider-tool cycle until done
  • Streams events through channels so a UI can show progress in real-time
  • Tests deterministically with a MockProvider that returns canned responses
  • Enforces safety with a permission engine, safety checks, and hooks
  • Loads project instructions from CLAUDE.md files and layered config

Architecture

The starter codebase uses a flat module layout:

mini-claw-code-starter/src/
  types.rs          -- Messages, tools, ToolSet, Provider trait, TokenUsage
  agent.rs          -- SimpleAgent (the core agent loop) and AgentEvent
  mock.rs           -- MockProvider for deterministic testing
  streaming.rs      -- SSE parsing, StreamAccumulator
  instructions.rs   -- InstructionLoader (CLAUDE.md discovery)
  permissions.rs    -- PermissionEngine
  safety.rs         -- SafetyChecker, SafeToolWrapper
  hooks.rs          -- Hook trait, HookRegistry
  planning.rs       -- PlanAgent (two-phase plan/execute)
  config.rs         -- Config, ConfigLoader, CostTracker
  context.rs        -- SystemPromptBuilder
  providers/
    openrouter.rs   -- OpenRouterProvider (real HTTP backend)
  tools/            -- Tool implementations (bash, file read/write/edit)

How to use this book

Start with Chapters 1-3. Three short, hands-on chapters get you from zero to a working agent in under an hour:

  1. Your First LLM Call — implement MockProvider (test_mock_)
  2. Your First Tool Call — implement ReadTool (test_read_)
  3. The Agentic Loop — implement single_turn and SimpleAgent (test_single_turn_, test_simple_agent_)

Then continue with Chapters 4-18 for the full architecture: streaming, permissions, hooks, plan mode, configuration, and more.

The mini-claw-code-starter crate contains stub implementations with unimplemented!() markers and doc comments describing what to do. Read the chapter, fill in the stubs, then verify your work by running the tests.

Run tests to check your progress:

# Run tests for a specific chapter (use the correct test name from the table below)
cargo test -p mini-claw-code-starter test_mock_

# Run all tests
cargo test -p mini-claw-code-starter

Prerequisites

  • Rust (edition 2024, 1.85+)
  • Basic familiarity with async Rust (async/await, tokio)
  • An OpenRouter API key (for the live provider chapters)

Chapter roadmap

Getting Started

ChapterTopicFile(s) to editTest command
1Your First LLM Callsrc/mock.rstest_mock_
2Your First Tool Callsrc/tools/read.rstest_read_
3The Agentic Loopsrc/agent.rstest_single_turn_, test_simple_agent_

Part I: Core Agent

ChapterTopicFile(s) to editTest command
4Messages & Typessrc/types.rs (pre-filled)test_mock_
5aProvider & Streaming Foundationssrc/mock.rs, src/streaming.rstest_mock_, test_streaming_parse_, test_streaming_accumulator_
5bOpenRouter & StreamingAgentsrc/providers/openrouter.rs, src/streaming.rstest_openrouter_, test_streaming_stream_chat_, test_streaming_streaming_agent_
6Tool Interfacesrc/tools/read.rs (already done in Ch2 — re-read)test_read_
7The Agentic Loop (Deep Dive)src/agent.rs (already done in Ch3 — re-read)test_single_turn_, test_simple_agent_

Part II: Prompt & Tools

ChapterTopicFile(s) to editTest command
8System Promptsrc/instructions.rsinstructions
9File Toolssrc/tools/write.rs, src/tools/edit.rs (read.rs already done in Ch2)test_read_, test_write_, test_edit_
10Bash Toolsrc/tools/bash.rstest_bash_
11Search Tools(extension -- no stubs)(no tests)
12Tool Registrysrc/types.rs (ToolSet — pre-filled, re-read)test_multi_tool_

Part III: Safety & Control

ChapterTopicFile(s) to editTest command
13Permission Enginesrc/permissions.rspermissions
14Safety Checkssrc/safety.rssafety
15Hookssrc/hooks.rshooks
16Plan Modesrc/planning.rsplan

Part IV: Configuration

ChapterTopicFile(s) to editTest command
17Settings Hierarchysrc/config.rs, src/usage.rsconfig, cost_tracker
18Project Instructionssrc/instructions.rs, src/context.rsinstructions, context_manager

Bonus (no chapter yet -- stubs + tests available)

TopicFile to editTest command
AskTool (user input)src/tools/ask.rsask (run with --ignored)
SubagentTool (child agents)src/subagent.rssubagent (run with --ignored)
Interactive CLIexamples/chat.rscargo run --example chat (after stub is filled in)

Let's start building.