Build Coding Agent Tutorial banner
happydog-intj happydog-intj

Build Coding Agent Tutorial

Development community

Description

从零构建 Coding Agent — 18 章 TypeScript 教程,理解 Claude Code / Cursor 背后的原理。Agent Loop、Tool Use、上下文管理、评测体系完整实现。

Installation

This entry records only its repository, not the path inside it, so there is no exact command to give. Open the source below and copy the folder into ~/.claude/skills/, or the file into ~/.claude/agents/.

README

从零构建 Coding Agent — TypeScript 教程

[![GitHub stars](https://img.shields.io/github/stars/happydog-intj/build-coding-agent-tutorial?style=social)](https://github.com/happydog-intj/build-coding-agent-tutorial) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE) [![Pages](https://img.shields.io/badge/在线阅读-GitHub%20Pages-brightgreen)](https://happydog-intj.github.io/build-coding-agent-tutorial/)

18 章渐进式教程 + 17 个可运行 Demo,理解 Claude Code / Cursor / Cline 背后的原理

📖 **[在线阅读](https://happydog-intj.github.io/build-coding-agent-tutorial/)** · 🪞 **[国内镜像](https://build-coding-agent-tutorial.vercel.app)** · 📥 **[下载 PDF](./releases/build-coding-agent-tutorial.pdf)** · 💬 **[FAQ](https://happydog-intj.github.io/build-coding-agent-tutorial/faq)**


用 TypeScript 从零实现一个完整的 AI Coding Agent(750 行),逐步讲解 Agent Loop、Tool Use、上下文管理、评测体系等核心原理。

快速开始

# 设置 API Key(二选一)
export ANTHROPIC_API_KEY=sk-ant-...
# 或
export OPENAI_API_KEY=sk-...

# 安装依赖
npm install

# 运行
npm start

# 指定模型
npm start -- --model=gpt-4o

架构概览

┌─────────────────────────────────────────────────────┐
│  index.ts (CLI 入口)                                 │
│  readline 交互 + 流式输出 + 会话管理                    │
├─────────────────────────────────────────────────────┤
│  agent-loop.ts (核心 Agent Loop)                     │
│  [用户消息] → LLM → 有 tool call? → 执行 → 重复      │
├─────────────────────────────────────────────────────┤
│  tools/ (4 个核心工具)                                │
│  read_file | write_file | edit_file | bash           │
├─────────────────────────────────────────────────────┤
│  provider.ts (LLM Provider)                          │
│  pi-ai: 30+ provider 统一接口                         │
├─────────────────────────────────────────────────────┤
│  session.ts (JSONL 持久化)                            │
│  增量追加、崩溃安全、简单恢复                            │
└─────────────────────────────────────────────────────┘

核心设计原则

这些原则来自 Pi 项目 5600+ 次迭代的经验:

1. LLM 调用永不 throw