Files

1 line
26 KiB
JSON
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{"repo": "ainlper/codingagent", "files": {"README.md": {"9fd8157aa9ac2d929d29137f679da92ba2cd6972": {"t": 1785674038.0456638, "n": 8997, "h": "284bdbd35261576c", "c": "# CodingAgent\n\n> Formerly **NanoCoder** — renamed to avoid confusion with [Nano-Collective/nanocoder](https://github.com/Nano-Collective/nanocoder). All links from the old repo redirect here automatically.\n\n\n[![PyPI](https://img.shields.io/pypi/v/codingagent)](https://pypi.org/project/codingagent/)\n[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)\n[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)\n[![Tests](https://github.com/he-yufeng/CodingAgent/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CodingAgent/actions)\n\n[中文](README_CN.md) | [English](README.md) | [Claude Code Architecture Deep Dive (7 articles)](article/)\n\n**512,000 lines of TypeScript → ~1,400 lines of Python.**\n\nI spent two days reverse-engineering the leaked Claude Code source — all half a million lines. Then I stripped it down to the load-bearing walls and rebuilt them in Python. The result: **every key architectural pattern from Claude Code, in a codebase you can read in one sitting.**\n\nCodingAgent is not another AI coding tool. It's a **blueprint** — the [nanoGPT](https://github.com/karpathy/nanoGPT) of coding agents. Read it, fork it, build your own.\n\n---\n\n```\n$ codingagent -m kimi-k2.5\n\nYou > read main.py and fix the broken import\n\n > read_file(file_path='main.py')\n > edit_file(file_path='main.py', ...)\n\n--- a/main.py\n+++ b/main.py\n@@ -1 +1 @@\n-from utils import halper\n+from utils import helper\n\nFixed: halper → helper.\n```\n\n## What You Get\n\nClaude Code's 512K lines distilled into ~1,400 lines across 7 patterns that actually matter:\n\n| Pattern | Claude Code | CodingAgent |\n|---|---|---|\n| Search-and-replace editing (unique match + diff) | FileEditTool | `tools/edit.py` — 70 lines |\n| Parallel tool execution | StreamingToolExecutor (530 lines) | `agent.py` — ThreadPool |\n| 3-layer context compression | HISTORY_SNIP → Microcompact → CONTEXT_COLLAPSE | `context.py` — 145 lines |\n| Sub-agent with isolated context | AgentTool (1,397 lines) | `tools/agent.py` — 50 lines |\n| Dangerous command blocking | BashTool (1,143 lines) | `tools/bash.py` — 95 lines |\n| Session persistence | QueryEngine (1,295 lines) | `session.py` — 65 lines |\n| Dynamic system prompt | prompts.ts (914 lines) | `prompt.py` — 35 lines |\n\nEvery pattern is a real, runnable implementation — not a diagram or a blog post.\n\n## Install\n\n```bash\ngit clone https://github.com/he-yufeng/CodingAgent.git\ncd CodingAgent\npip install -e .\n```\n\nThe installed command is lowercase: `codingagent`. On Linux and macOS, `CodingAgent` is a different name and will not resolve.\n\nPick your model — any OpenAI-compatible API works. You can `export` env vars or drop a `.env` file in your project root:\n\n```bash\n# Kimi K2.5\nexport OPENAI_API_KEY=your-key OPENAI_BASE_URL=https://api.moonshot.ai/v1\ncodingagent -m kimi-k2.5\n\n# Claude Opus 4.6 (via OpenRouter)\nexport OPENAI_API_KEY=your-key OPENAI_BASE_URL=https://openrouter.ai/api/v1\ncodingagent -m anthropic/claude-opus-4-6\n\n# OpenAI GPT-5\nexport OPENAI_API_KEY=sk-...\ncodingagent -m gpt-5\n\n# DeepSeek V3\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://api.deepseek.com\ncodingagent -m deepseek-chat\n\n# Qwen 3.5\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1\ncodingagent -m qwen-max\n\n# Ollama (local)\nexport OPENAI_API_KEY=ollama OPENAI_BASE_URL=http://localhost:11434/v1\ncodingagent -m qwen3:32b\n\n# One-shot mode\ncodingagent -p \"add error handling to parse_config()\"\n```\n\n### Non-OpenAI providers (Bedrock, Vertex, Cohere, …)\n\nFor providers without an OpenAI-compatible endpoint, install the optional LiteLLM extra:\n\n```bash\npip install -e '.[litellm]'\n\nexport CODINGAGENT_PROVIDER=litellm\nexport CODINGAGENT_MODEL=anthropic/claude-3-haiku # any LiteLLM model string\nexport ANTHROPIC_API_KEY=sk-ant-...\ncodingagent\n```\n\nLiteLLM routes through to 100+ providers (Bedrock, Vertex AI, Cohere, Groq, Replicate, Anyscale, etc.) using one model-string convention. The default `openai` backend is unchanged.\n\n## Architecture\n\nThe whole thing fits in your head:\n\n```\ncodingagent/\n├── cli.py REPL + commands 218 lines\n├── agent.py Agent loop + parallel tools 122 lines\n├── llm.py Streaming client + retry 156 lines\n├── context.py 3-layer compression 196 lines\n├── session.py Save/resume 68 lines\n├── prompt.py System prompt 33 lines\n├── config.py Env config 55 lines\n└── tools/\n ├── bash.py Shell + safety + cd tracking 115 lines\n ├── edit.py Search-replace + diff 85 lines\n ├── read.py File reading 53 lines\n ├── write.py File writing 36 lines\n ├── glob_tool.py File search 47 lines\n ├── grep.py Content search 78 lines\n └── agent.py Sub-agent spawning 58 lines\n```\n\n## Use as a Library\n\n```python\nfrom codingagent import Agent, LLM\n\nllm = LLM(model=\"kimi-k2.5\", api_key=\"your-key\", base_url=\"https://api.moonshot.ai/v1\")\nagent = Agent(llm=llm)\nresponse = agent.chat(\"find all TODO comments in this project and list them\")\n```\n\n## Add Your Own Tools (~20 lines)\n\n```python\nfrom codingagent.tools.base import Tool\n\nclass HttpTool(Tool):\n name = \"http\"\n description = \"Fetch a URL.\"\n parameters = {\"type\": \"object\", \"properties\": {\"url\": {\"type\": \"string\"}}, \"required\": [\"url\"]}\n\n def execute(self, url: str) -> str:\n import urllib.request\n return urllib.request.urlopen(url).read().decode()[:5000]\n```\n\n## Commands\n\n```\n/model Show current model\n/model <name> Switch model mid-conversation\n/compact Compress context (like Claude Code's /compact)\n/tokens Token usage + cost estimate\n/diff Show files modified this session\n/save Save session to disk\n/sessions List saved sessions\n/reset Clear history\nquit Exit\n```\n\nSaved session IDs are sanitized before they become filenames, so resume data stays inside `~/.codingagent/sessions`.\n\n## How It Compares\n\n| | Claude Code | Claw-Code | Aider | CodingAgent |\n|---|---|---|---|---|\n| Code | 512K lines (closed) | 100K+ lines | 50K+ lines | **~1,400 lines** |\n| Models | Anthropic only | Multi | Multi | **Any OpenAI-compatible** |\n| Readable? | No | Hard | Medium | **One afternoon** |\n| Purpose | Use it | Use it | Use it | **Understand it, build yours** |\n\n## The Deep Dive\n\nI wrote [7 articles](article/) breaking down Claude Code's architecture — the agent loop, tool system, context compression, streaming executor, multi-agent, and 44 hidden feature flags. If you want to understand *why* CodingAgent is designed this way, start there.\n\n## FAQ\n\n**Does CodingAgent support Skills / Subagents / MCP?**\n\nNo, and that's intentional. CodingAgent is the minimal runnable core — agent loop, tools, streaming, compaction. Skills, Subagents, MCP, hooks, and plugins are upper-layer features that Claude Code layers on top; if CodingAgent had them too it would stop being a teaching artifact. The architecture articles above cover how those systems work in Claude Code, so you can add them yourself if you need to.\n\nIf you want Skills specifically, the recipe is small: scan `~/.claude/skills/*.md` at startup, list their titles in the system prompt, and let the agent ask for a skill by name before you inline that file's body into the conversation.\n\n## Related Projects\n\n- **[CodeJoust](https://github.com/he-yufeng/CodeJoust)** — a CLI arena that races Claude Code, aider, Codex, and Gemini (Cursor + OpenHands next) on the same bug in isolated git worktrees, scores by tests+cost+diff+time, hands you the winning patch. If you ever wondered *which* AI coding CLI is actually better for your task, CodeJoust answers it empirically.\n- **[AnyCoder](https://github.com/he-yufeng/AnyCoder)** — a practical terminal AI coding agent built on the same architecture as CodingAgent but with litellm, session persistence, and 100+ model support. Use this one if you want a tool; use CodingAgent if you want to read source.\n- **[LiteBench](https://github.com/he-yufeng/LiteBench)** — one-command LLM / agent benchmark. Ships 7 built-in tasks (HumanEval/GSM8K/MMLU/...) and YAML-defined custom tasks, with a single-file HTML dashboard.\n- **[RepoWiki](https://github.com/he-yufeng/RepoWiki)** — open-source DeepWiki alternative. `pip install repowiki`, one command to turn any local or GitHub repo into a wiki with dependency graph, architecture diagram, and LLM-generated module pages.\n\n## License\n\nMIT. Fork it, learn from it, ship something better. A mention of this project is appreciated.\n\n---\n\nBuilt by **[Yufeng He](https://github.com/he-yufeng)** · Agentic AI Researcher @ Moonshot AI (Kimi)\n\n[Claude Code Source Analysis — 170K+ reads, 6000 bookmarks on Zhihu](https://zhuanlan.zhihu.com/p/1898797658343862272)\n"}, "a95958806f04472c3149cf1c710c2dd2ee9792b0": {"t": 1785763941.9616423, "n": 8997, "h": "284bdbd35261576c", "c": "# CodingAgent\n\n> Formerly **NanoCoder** — renamed to avoid confusion with [Nano-Collective/nanocoder](https://github.com/Nano-Collective/nanocoder). All links from the old repo redirect here automatically.\n\n\n[![PyPI](https://img.shields.io/pypi/v/codingagent)](https://pypi.org/project/codingagent/)\n[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)\n[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)\n[![Tests](https://github.com/he-yufeng/CodingAgent/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CodingAgent/actions)\n\n[中文](README_CN.md) | [English](README.md) | [Claude Code Architecture Deep Dive (7 articles)](article/)\n\n**512,000 lines of TypeScript → ~1,400 lines of Python.**\n\nI spent two days reverse-engineering the leaked Claude Code source — all half a million lines. Then I stripped it down to the load-bearing walls and rebuilt them in Python. The result: **every key architectural pattern from Claude Code, in a codebase you can read in one sitting.**\n\nCodingAgent is not another AI coding tool. It's a **blueprint** — the [nanoGPT](https://github.com/karpathy/nanoGPT) of coding agents. Read it, fork it, build your own.\n\n---\n\n```\n$ codingagent -m kimi-k2.5\n\nYou > read main.py and fix the broken import\n\n > read_file(file_path='main.py')\n > edit_file(file_path='main.py', ...)\n\n--- a/main.py\n+++ b/main.py\n@@ -1 +1 @@\n-from utils import halper\n+from utils import helper\n\nFixed: halper → helper.\n```\n\n## What You Get\n\nClaude Code's 512K lines distilled into ~1,400 lines across 7 patterns that actually matter:\n\n| Pattern | Claude Code | CodingAgent |\n|---|---|---|\n| Search-and-replace editing (unique match + diff) | FileEditTool | `tools/edit.py` — 70 lines |\n| Parallel tool execution | StreamingToolExecutor (530 lines) | `agent.py` — ThreadPool |\n| 3-layer context compression | HISTORY_SNIP → Microcompact → CONTEXT_COLLAPSE | `context.py` — 145 lines |\n| Sub-agent with isolated context | AgentTool (1,397 lines) | `tools/agent.py` — 50 lines |\n| Dangerous command blocking | BashTool (1,143 lines) | `tools/bash.py` — 95 lines |\n| Session persistence | QueryEngine (1,295 lines) | `session.py` — 65 lines |\n| Dynamic system prompt | prompts.ts (914 lines) | `prompt.py` — 35 lines |\n\nEvery pattern is a real, runnable implementation — not a diagram or a blog post.\n\n## Install\n\n```bash\ngit clone https://github.com/he-yufeng/CodingAgent.git\ncd CodingAgent\npip install -e .\n```\n\nThe installed command is lowercase: `codingagent`. On Linux and macOS, `CodingAgent` is a different name and will not resolve.\n\nPick your model — any OpenAI-compatible API works. You can `export` env vars or drop a `.env` file in your project root:\n\n```bash\n# Kimi K2.5\nexport OPENAI_API_KEY=your-key OPENAI_BASE_URL=https://api.moonshot.ai/v1\ncodingagent -m kimi-k2.5\n\n# Claude Opus 4.6 (via OpenRouter)\nexport OPENAI_API_KEY=your-key OPENAI_BASE_URL=https://openrouter.ai/api/v1\ncodingagent -m anthropic/claude-opus-4-6\n\n# OpenAI GPT-5\nexport OPENAI_API_KEY=sk-...\ncodingagent -m gpt-5\n\n# DeepSeek V3\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://api.deepseek.com\ncodingagent -m deepseek-chat\n\n# Qwen 3.5\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1\ncodingagent -m qwen-max\n\n# Ollama (local)\nexport OPENAI_API_KEY=ollama OPENAI_BASE_URL=http://localhost:11434/v1\ncodingagent -m qwen3:32b\n\n# One-shot mode\ncodingagent -p \"add error handling to parse_config()\"\n```\n\n### Non-OpenAI providers (Bedrock, Vertex, Cohere, …)\n\nFor providers without an OpenAI-compatible endpoint, install the optional LiteLLM extra:\n\n```bash\npip install -e '.[litellm]'\n\nexport CODINGAGENT_PROVIDER=litellm\nexport CODINGAGENT_MODEL=anthropic/claude-3-haiku # any LiteLLM model string\nexport ANTHROPIC_API_KEY=sk-ant-...\ncodingagent\n```\n\nLiteLLM routes through to 100+ providers (Bedrock, Vertex AI, Cohere, Groq, Replicate, Anyscale, etc.) using one model-string convention. The default `openai` backend is unchanged.\n\n## Architecture\n\nThe whole thing fits in your head:\n\n```\ncodingagent/\n├── cli.py REPL + commands 218 lines\n├── agent.py Agent loop + parallel tools 122 lines\n├── llm.py Streaming client + retry 156 lines\n├── context.py 3-layer compression 196 lines\n├── session.py Save/resume 68 lines\n├── prompt.py System prompt 33 lines\n├── config.py Env config 55 lines\n└── tools/\n ├── bash.py Shell + safety + cd tracking 115 lines\n ├── edit.py Search-replace + diff 85 lines\n ├── read.py File reading 53 lines\n ├── write.py File writing 36 lines\n ├── glob_tool.py File search 47 lines\n ├── grep.py Content search 78 lines\n └── agent.py Sub-agent spawning 58 lines\n```\n\n## Use as a Library\n\n```python\nfrom codingagent import Agent, LLM\n\nllm = LLM(model=\"kimi-k2.5\", api_key=\"your-key\", base_url=\"https://api.moonshot.ai/v1\")\nagent = Agent(llm=llm)\nresponse = agent.chat(\"find all TODO comments in this project and list them\")\n```\n\n## Add Your Own Tools (~20 lines)\n\n```python\nfrom codingagent.tools.base import Tool\n\nclass HttpTool(Tool):\n name = \"http\"\n description = \"Fetch a URL.\"\n parameters = {\"type\": \"object\", \"properties\": {\"url\": {\"type\": \"string\"}}, \"required\": [\"url\"]}\n\n def execute(self, url: str) -> str:\n import urllib.request\n return urllib.request.urlopen(url).read().decode()[:5000]\n```\n\n## Commands\n\n```\n/model Show current model\n/model <name> Switch model mid-conversation\n/compact Compress context (like Claude Code's /compact)\n/tokens Token usage + cost estimate\n/diff Show files modified this session\n/save Save session to disk\n/sessions List saved sessions\n/reset Clear history\nquit Exit\n```\n\nSaved session IDs are sanitized before they become filenames, so resume data stays inside `~/.codingagent/sessions`.\n\n## How It Compares\n\n| | Claude Code | Claw-Code | Aider | CodingAgent |\n|---|---|---|---|---|\n| Code | 512K lines (closed) | 100K+ lines | 50K+ lines | **~1,400 lines** |\n| Models | Anthropic only | Multi | Multi | **Any OpenAI-compatible** |\n| Readable? | No | Hard | Medium | **One afternoon** |\n| Purpose | Use it | Use it | Use it | **Understand it, build yours** |\n\n## The Deep Dive\n\nI wrote [7 articles](article/) breaking down Claude Code's architecture — the agent loop, tool system, context compression, streaming executor, multi-agent, and 44 hidden feature flags. If you want to understand *why* CodingAgent is designed this way, start there.\n\n## FAQ\n\n**Does CodingAgent support Skills / Subagents / MCP?**\n\nNo, and that's intentional. CodingAgent is the minimal runnable core — agent loop, tools, streaming, compaction. Skills, Subagents, MCP, hooks, and plugins are upper-layer features that Claude Code layers on top; if CodingAgent had them too it would stop being a teaching artifact. The architecture articles above cover how those systems work in Claude Code, so you can add them yourself if you need to.\n\nIf you want Skills specifically, the recipe is small: scan `~/.claude/skills/*.md` at startup, list their titles in the system prompt, and let the agent ask for a skill by name before you inline that file's body into the conversation.\n\n## Related Projects\n\n- **[CodeJoust](https://github.com/he-yufeng/CodeJoust)** — a CLI arena that races Claude Code, aider, Codex, and Gemini (Cursor + OpenHands next) on the same bug in isolated git worktrees, scores by tests+cost+diff+time, hands you the winning patch. If you ever wondered *which* AI coding CLI is actually better for your task, CodeJoust answers it empirically.\n- **[AnyCoder](https://github.com/he-yufeng/AnyCoder)** — a practical terminal AI coding agent built on the same architecture as CodingAgent but with litellm, session persistence, and 100+ model support. Use this one if you want a tool; use CodingAgent if you want to read source.\n- **[LiteBench](https://github.com/he-yufeng/LiteBench)** — one-command LLM / agent benchmark. Ships 7 built-in tasks (HumanEval/GSM8K/MMLU/...) and YAML-defined custom tasks, with a single-file HTML dashboard.\n- **[RepoWiki](https://github.com/he-yufeng/RepoWiki)** — open-source DeepWiki alternative. `pip install repowiki`, one command to turn any local or GitHub repo into a wiki with dependency graph, architecture diagram, and LLM-generated module pages.\n\n## License\n\nMIT. Fork it, learn from it, ship something better. A mention of this project is appreciated.\n\n---\n\nBuilt by **[Yufeng He](https://github.com/he-yufeng)** · Agentic AI Researcher @ Moonshot AI (Kimi)\n\n[Claude Code Source Analysis — 170K+ reads, 6000 bookmarks on Zhihu](https://zhuanlan.zhihu.com/p/1898797658343862272)\n"}}, "README_CN.md": {"9fd8157aa9ac2d929d29137f679da92ba2cd6972": {"t": 1785674235.584837, "n": 5383, "h": "3928bcfa5b904889", "c": "# CodingAgent\n\n> 原名 **NanoCoder**,为避免与 [Nano-Collective/nanocoder](https://github.com/Nano-Collective/nanocoder) 混淆而改名。旧链接自动跳转到这里。\n\n\n[English](README.md) | [中文](README_CN.md) | [Claude Code 源码深度导读(7 篇)](article/)\n\n[![PyPI](https://img.shields.io/pypi/v/codingagent)](https://pypi.org/project/codingagent/)\n[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)\n[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)\n[![Tests](https://github.com/he-yufeng/CodingAgent/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CodingAgent/actions)\n\n**51万行 TypeScript → ~1,400 行 Python。**\n\n我逆向了 Claude Code 泄露的全部源码,然后把不承重的部分全扔掉,用 Python 重建了核心。成果:**Claude Code 的每一个关键架构模式,浓缩在一个下午能读完的代码库里。**\n\nCodingAgent 不仅是一个 AI 编程工具。它是一份**蓝图**,编程 Agent 领域的 [nanoGPT](https://github.com/karpathy/nanoGPT)。读懂它,fork 它,然后造你自己的。\n\n---\n\n```\n$ codingagent -m kimi-k2.5\n\nYou > 读一下 main.py,修掉拼错的 import\n\n > read_file(file_path='main.py')\n > edit_file(file_path='main.py', ...)\n\n--- a/main.py\n+++ b/main.py\n@@ -1 +1 @@\n-from utils import halper\n+from utils import helper\n\n修好了:halper → helper。\n```\n\n## 你能得到什么\n\nClaude Code 51 万行源码提炼出来的 7 个核心模式:\n\n| 设计模式 | Claude Code | CodingAgent |\n|---|---|---|\n| 搜索替换编辑(唯一匹配 + diff) | FileEditTool | `tools/edit.py` — 70 行 |\n| 并行工具执行 | StreamingToolExecutor(530行) | `agent.py` — ThreadPool |\n| 三层上下文压缩 | HISTORY_SNIP → Microcompact → CONTEXT_COLLAPSE | `context.py` — 145 行 |\n| 子代理隔离上下文 | AgentTool(1,397行) | `tools/agent.py` — 50 行 |\n| 危险命令拦截 | BashTool(1,143行) | `tools/bash.py` — 95 行 |\n| 会话持久化 | QueryEngine(1,295行) | `session.py` — 65 行 |\n| 动态系统提示词 | prompts.ts(914行) | `prompt.py` — 35 行 |\n\n每个模式都是可运行的实现,不是流程图,不是博客文章。\n\n## 安装\n\n```bash\ngit clone https://github.com/he-yufeng/CodingAgent.git\ncd CodingAgent\npip install -e .\n```\n\n安装后的命令是小写的 `codingagent`。在 Linux 和 macOS 上,`CodingAgent` 是另一个名字,不会被识别。\n\n选你的模型,任何 OpenAI 兼容 API 都行。可以 `export` 环境变量,也可以在项目根目录放一个 `.env` 文件:\n\n```bash\n# Kimi K2.5\nexport OPENAI_API_KEY=你的key OPENAI_BASE_URL=https://api.moonshot.ai/v1\ncodingagent -m kimi-k2.5\n\n# Claude Opus 4.6(通过 OpenRouter)\nexport OPENAI_API_KEY=你的key OPENAI_BASE_URL=https://openrouter.ai/api/v1\ncodingagent -m anthropic/claude-opus-4-6\n\n# OpenAI GPT-5\nexport OPENAI_API_KEY=sk-...\ncodingagent -m gpt-5\n\n# DeepSeek V3\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://api.deepseek.com\ncodingagent -m deepseek-chat\n\n# Qwen 3.5\nexport OPENAI_API_KEY=sk-... OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1\ncodingagent -m qwen-max\n\n# Ollama(本地)\nexport OPENAI_API_KEY=ollama OPENAI_BASE_URL=http://localhost:11434/v1\ncodingagent -m qwen3:32b\n\n# 单次模式\ncodingagent -p \"给 parse_config() 加上错误处理\"\n```\n\n## 架构\n\n整个项目一目了然:\n\n```\ncodingagent/\n├── cli.py REPL + 命令 218 行\n├── agent.py Agent 循环 + 并行执行 122 行\n├── llm.py 流式客户端 + 重试 156 行\n├── context.py 三层压缩 196 行\n├── session.py 会话保存/恢复 68 行\n├── prompt.py 系统提示词 33 行\n├── config.py 环境变量配置 55 行\n└── tools/\n ├── bash.py Shell + 安全 + cd 追踪 115 行\n ├── edit.py 搜索替换 + diff 85 行\n ├── read.py 文件读取 53 行\n ├── write.py 文件写入 36 行\n ├── glob_tool.py 文件搜索 47 行\n ├── grep.py 内容搜索 78 行\n └── agent.py 子代理生成 58 行\n```\n\n## 当库用\n\n```python\nfrom codingagent import Agent, LLM\n\nllm = LLM(model=\"kimi-k2.5\", api_key=\"your-key\", base_url=\"https://api.moonshot.ai/v1\")\nagent = Agent(llm=llm)\nresponse = agent.chat(\"找出项目里所有 TODO 注释并列出来\")\n```\n\n## 加自定义工具(约 20 行)\n\n```python\nfrom codingagent.tools.base import Tool\n\nclass HttpTool(Tool):\n name = \"http\"\n description = \"请求一个 URL。\"\n parameters = {\"type\": \"object\", \"properties\": {\"url\": {\"type\": \"string\"}}, \"required\": [\"url\"]}\n\n def execute(self, url: str) -> str:\n import urllib.request\n return urllib.request.urlopen(url).read().decode()[:5000]\n```\n\n## 命令\n\n```\n/model 查看当前模型\n/model <名称> 切换模型\n/compact 压缩上下文(对标 Claude Code 的 /compact)\n/tokens 查看 token 用量 + 费用估算\n/diff 查看本次会话修改的文件\n/save 保存会话\n/sessions 列出已保存的会话\n/reset 清空历史\nquit 退出\n```\n\n保存的会话 ID 会先安全化再作为文件名,恢复数据始终留在 `~/.codingagent/sessions` 目录内。\n\n## 对比\n\n| | Claude Code | Claw-Code | Aider | CodingAgent |\n|---|---|---|---|---|\n| 代码量 | 51万行(闭源) | 10万+行 | 5万+行 | **~1,400 行** |\n| 模型 | 仅 Anthropic | 多模型 | 多模型 | **任意 OpenAI 兼容** |\n| 能通读吗? | 不能 | 很难 | 有点费劲 | **一个下午** |\n| 适合 | 直接用 | 直接用 | 直接用 | **先看懂,再造自己的** |\n\n## 源码导读\n\n我还写了 [7 篇 Claude Code 架构深度导读](article/):Agent 循环、工具系统、上下文压缩、流式执行、多 Agent、隐藏功能。想知道 CodingAgent 为什么这样设计,从那里开始。\n\n## FAQ\n\n**CodingAgent 支持 Skill / Subagent / MCP 吗?**\n\n不支持,这是刻意的。CodingAgent 只保留可运行的最小核心 —— agent 循环、工具、流式、压缩。Skill、Subagent、MCP、hook、plugin 都是 Claude Code 在上层加的特性;如果 CodingAgent 也全都做了,就不再是一个可读的教学产物。上面的架构导读系列讲了 Claude Code 里这些系统是怎么工作的,你可以照着自己加。\n\n如果你只是想要 Skill,配方很简单:启动时扫 `~/.claude/skills/*.md`,把标题列进 system prompt,让 agent 按名字请求某个 skill,再把那个文件的内容 inline 进对话就行了。\n\n## License\n\nMIT。Fork,然后拿去造更好的东西,如果能标注此出处就更好了。\n\n---\n\n作者 **[何宇峰](https://github.com/he-yufeng)** · Agentic AI Researcher @ Moonshot AI (Kimi)\n\n[Claude Code 源码分析(知乎 17 万阅读,6000收藏)](https://zhuanlan.zhihu.com/p/1898797658343862272)\n"}}}}