> For the complete documentation index, see [llms.txt](https://laynepeng.gitbook.io/ai-code-agent-shou-ba-shou/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://laynepeng.gitbook.io/ai-code-agent-shou-ba-shou/readme.md).

# 《AI Code Agent 手把手》— lite-code 从零实战

> 一套从零手写 Code Agent 的完整教程。读完 24 课，你将亲手造出 [lite-code](https://github.com/LaynePeng/lite-code) —— 一个可运行的桌面 Code Agent 应用（Python 内核 + React UI + Electron 外壳）。

## 你将学到

* **不依赖 LangChain 等高层框架**，纯手写 Agent 内核
* LLM 流式 Tool Calling 底层解析、JSON 自愈、死循环检测、Token 预算
* **Prompt 缓存机制**（命中率优化、断点标注、稳定前缀）与 **Token 节省策略**（多层预算、带外存储）
* 代码理解（检索机制 / Tree-sitter AST）、安全代码操作（沙箱隔离 / 精确编辑）
* Skills 系统与权限治理、MCP 协议、插件架构（微内核 / 依赖注入）
* 多 Agent 协作、Build/Plan 双默认 Agent 与用户自定义 Agent 机制
* 多 LLM 供应商接入、FastAPI 服务、React UI、Electron 桌面打包
* 支持全局默认模型与当前会话独立切换模型，主界面显示实际生效的模型与推理档位
* stdio MCP Client、动态工具发现与工具调用
* Electron 真实终端（macOS 使用 shell，Windows 使用 PowerShell）与多窗口项目工作区
* 支持项目根目录 `AGENTS.md` 和 `Claude.md` 指令文件
* 支持项目级/用户级 `skills/*/SKILL.md`，通过 `load_skill` 按需加载技能

## 课程目录

| 模块                | 课程                                                                                                                                           | 主题                                                |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| **模块一：ReAct 与循环** | [第 1 课](/ai-code-agent-shou-ba-shou/01-di-1-ke-llm-yuan-sheng-jie-kou-feng-zhuang-yu-tool-calling-di-ceng-liu-shi-jie-xi.md)                 | LLM 原生接口封装 + Tool Calling 流式解析                    |
|                   | [第 2 课](/ai-code-agent-shou-ba-shou/02-di-2-ke-agent-kong-zhi-xun-huan-de-yi-chang-rong-cuo-si-xun-huan-zhong-duan-yu-token-yu-suan-kong.md) | JSON 自愈 / 死循环检测 / 输出截断（OpenCode 风格）               |
|                   | [第 3 课](/ai-code-agent-shou-ba-shou/03-di-3-ke-shang-xia-wen-guan-li-token-dong-tai-ji-suan-yu-dynamic-system-prompt.md)                     | Token 预算 / 策略 B 两阶段裁剪 / 稳定 System Prompt          |
|                   | [第 4 课](/ai-code-agent-shou-ba-shou/04-di-4-ke-prompt-huan-cun-ji-zhi-ming-zhonglyou-hua-yu-gong-cheng-shi-jian.md)                          | Prompt 缓存 / 断点标注 / 稳定前缀设计                         |
|                   | [第 5 课](/ai-code-agent-shou-ba-shou/05-di-5-ke-token-jie-sheng-ce-le-cong-opensquilla-dao-huan-cun-you-hao.md)                               | 多层预算治理 / 四层压缩 / 带外存储 / 缓存铁律                       |
| **模块二：代码理解与安全操作** | [第 6 课](/ai-code-agent-shou-ba-shou/06-di-6-ke-dai-ma-li-jie-jian-suo-ji-zhi-yu-jie-gou-fen-xi.md)                                           | Ripgrep 检索 / 文件树 / Tree-sitter AST / 骨架压缩         |
|                   | [第 7 课](/ai-code-agent-shou-ba-shou/07-di-7-ke-an-quan-dai-ma-cao-zuo-zhi-xing-ge-li-yu-yuan-zi-bian-ji.md)                                  | 沙箱隔离 / Search-Replace / Unified Diff              |
|                   | [第 8 课](/ai-code-agent-shou-ba-shou/08-di-8-ke-skills-xi-tong-zhu-ru-ji-zhi-yu-quan-xian-zhi-li.md)                                          | AGENTS.md / CLAUDE.md / Skills 按需加载 / 权限治理        |
|                   | [第 9 课](/ai-code-agent-shou-ba-shou/09-di-9-ke-biao-zhun-model-context-protocolmcp-jie-ru.md)                                                | MCP 协议接入                                          |
| **模块三：核心架构**      | [第 10 课](/ai-code-agent-shou-ba-shou/10-di-10-ke-cha-jian-jia-gou-nei-he-she-ji-yu-yi-lai-zhu-ru.md)                                         | Cordis 微内核 / 时空解耦 / 洋葱中间件 / 生命周期钩子                |
|                   | [第 11 课](/ai-code-agent-shou-ba-shou/11-di-11-ke-duo-agent-xie-zuo-bian-pai-tong-xin-yu-chi-jiu-hua.md)                                      | 子 Agent 编排 / 审批透传 / 跨总线转发                         |
|                   | [第 12 课](/ai-code-agent-shou-ba-shou/12-di-12-ke-agent-lei-xing-yu-zi-ding-yi-ji-zhi.md)                                                     | Build/Plan 双默认 Agent / 自定义 Agent 机制               |
| **模块四：知识篇收尾**     | [第 13 课](/ai-code-agent-shou-ba-shou/13-di-13-ke-zhi-shi-pian-zong-he-fu-pan.md)                                                             | 知识篇综合复盘                                           |
| **模块五：手写实战**      | [第 14 课](/ai-code-agent-shou-ba-shou/14-di-14-ke-he-xin-core-nei-he-bian-ma-litecode-shi-zhan-di-yi-pian.md)                                 | Python Core 内核                                    |
|                   | [第 15 课](/ai-code-agent-shou-ba-shou/15-di-15-ke-llm-duo-gong-ying-shang-shi-pei-qi-yu-he-xin-gong-ju-ji-litecode-shi-zhan-di-er-pian.md)    | 多 LLM + models.dev 元数据 + 20 工具（含 Web 抓取 / Skills） |
|                   | [第 16 课](/ai-code-agent-shou-ba-shou/16-di-16-ke-agentloop-zhu-xun-huan-yu-zeng-qiang-ji-cheng-litecode-shi-zhan-di-san-pian.md)             | AgentLoop 主循环（策略 B 集成 + 上下文可观测性）                  |
|                   | [第 17 课](/ai-code-agent-shou-ba-shou/17-di-17-ke-agentloop-kuo-zhan-gong-ju-bing-xing-hua-pai-dui-shu-ru-llm-gu-zhang-chong-shi.md)          | 工具并行化 / 排队输入 / LLM 故障重试                           |
|                   | [第 18 课](/ai-code-agent-shou-ba-shou/18-di-18-ke-an-quan-sha-xiang-yu-gao-wei-lan-jie-shi-zhan-litecode-shi-zhan-di-si-pian.md)              | 安全沙箱 + Web 审批 + MCP 工具审批                          |
|                   | [第 19 课](/ai-code-agent-shou-ba-shou/19-di-19-ke-ren-ji-jiao-hu-shen-pi-ka-yu-askuser-ti-wen.md)                                             | 审批卡 / ask\_user 提问                                |
|                   | [第 20 课](/ai-code-agent-shou-ba-shou/20-di-20-ke-todo-qing-dan-xi-tong-gong-ju-cha-jian-shi-jian-qu-dong-yu-chi-jiu-hua.md)                  | TODO 清单系统 / 事件驱动 / 持久化                            |
|                   | [第 21 课](/ai-code-agent-shou-ba-shou/21-di-21-ke-web-ui-shi-zhan-litecode-shi-zhan-di-wu-pian.md)                                            | FastAPI + React Web UI（上下文情况面板 / 多 Tab）           |
|                   | [第 22 课](/ai-code-agent-shou-ba-shou/22-di-22-ke-hui-hua-guan-li-yu-zong-he-she-zhi.md)                                                      | 孤儿会话 / 项目切换 / 配置面板                                |
|                   | [第 23 课](/ai-code-agent-shou-ba-shou/23-di-23-ke-electron-zhuo-mian-ying-yong-litecode-shi-zhan-di-liu-pian.md)                              | 多窗口项目工作区 / 真实终端 / 打包发布                            |
|                   | [第 24 课](/ai-code-agent-shou-ba-shou/24-di-24-ke-gong-cheng-shi-jian-yu-cai-keng-litecode-shi-zhan-zhong-zhang.md)                           | 空响应重试 / 配置迁移 / 会话 ID / 强制交付报告                     |

## 学习路线

1. **理论入门（第 1-5 课）**：理解 Agent 循环、LLM 流式解析、上下文控制、缓存机制、Token 节省策略
2. **代码理解与安全操作（第 6-9 课）**：代码理解、安全代码操作、Skills 系统与权限治理、MCP 协议
3. **核心架构（第 10-13 课）**：插件架构、多 Agent 协作、Agent 类型、知识篇综合复盘
4. **手写实战（第 14-24 课）**：从零搭建完整的 lite-code 桌面应用，以工程实践与踩坑收官

## 运行最终项目

```bash
# 克隆 lite-code 项目
git clone https://github.com/LaynePeng/lite-code.git
cd lite-code

# 安装依赖
python3 -m venv .venv
.venv/bin/pip install -e .[dev]
npm install

# 一键启动（Windows 使用 .venv\Scripts\python.exe）
npm run dev
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://laynepeng.gitbook.io/ai-code-agent-shou-ba-shou/readme.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
