跪拜 Guibai
← All articles
Artificial Intelligence · DeepSeek · Agent

Inside a Homebuilt DeepSeek Coding Agent: It's Just a while(true) Loop

By 樊小肆 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Most developers treat AI assistants as black boxes; this breakdown shows exactly where decisions happen and where control and costs can leak. Building your own agent from the loop up makes it clear why tools like Claude Code behave the way they do, and what engineering choices actually separate a reliable agent from a toy.

Summary

The mystery behind Claude Code, Codex, and Cursor's agent modes is not emergence or planning magic. It's a while(true) loop: the model decides which tool to call, the engine executes it, feeds the result back, and the model repeats until it decides the task is finished. A spare-time project called DeepSeeker-Code implements this loop from scratch, with DeepSeek at the base and both CLI and VSCode interfaces.

The architecture has four layers: entry points, the agent kernel, a tool layer with 25+ built-in tools, and the model base. Tools carry safety levels: read-only tools run without approval, write tools prompt for diff confirmation, and dangerous commands force manual confirmation. Context management treats the window as short-term memory, compressing old messages into lossless entity indices and narrative summaries, then letting the model recall details with a dedicated tool rather than truncating abruptly.

Extensions come as Hooks (lifecycle scripts), Skills (on-demand SKILL.md files), and MCP (external tool servers). Sub-agents run in parallel with isolated contexts and git worktrees, inheriting budget caps. A live cost meter shows per-round spending, and an unattended mode runs overnight tasks with acceptance commands as the final gate. The author argues that any apparent "intelligence" reduces to how well each loop link is engineered.

Takeaways
— The core of every agent product is a loop: model reads context and decides to speak or call a tool; engine executes, feeds results back; repeat until no tool call is issued.
— DeepSeeker-Code is a from-scratch DeepSeek-powered agent with CLI and VSCode interfaces, using 25+ built-in tools plus MCP, Hooks, and Skills.
— Tools are tiered by safety: safe (read-only) runs without approval, mutation (writes) shows a diff prompt, danger (arbitrary commands) always requires manual confirmation.
— Context overflow is handled by compressing early messages into two slots: lossless regex-extracted entity indices and model-generated narrative summaries; the model can recall details with a recall tool that also checks whether files have changed since.
— Sub-agents have independent contexts and use git worktrees to isolate disk state, preventing interference; they inherit the parent's budget and automatically melt down at round boundaries if spending exceeds the cap.
— Extensions don't need kernel changes: Hooks run scripts at 11 lifecycle events, Skills are SKILL.md files loaded on demand, MCP servers expose tools as mcp__* automatically.
— A real-time cost meter displays session cost and budget, and tasks can have a hard budget cap that stops execution when exceeded.
— Unattended mode writes a task ticket and acceptance commands; the agent works serially overnight in an isolated worktree and only a passing acceptance command counts as done.
Conclusions

The claim that no framework or existing agent harness was used is a strong differentiator: most agent projects wrap existing libraries and hide the loop details; writing it from scratch forces an actual understanding of every failure mode.

Safety tiering treats model mistakes as an expected condition rather than an edge case, which is more honest than most agent designs that either trust the model entirely or block everything.

Context compression with recall is a pragmatic middle ground between naive truncation and full retry; the tradeoff between lossless indices and lossy summaries is a real design decision that most products avoid by just buying bigger windows.

Budget caps and per-round cost display turn an often hidden operational cost into a first-class control, which matters as a developer when agents run unattended for long jobs.

Sub-agent isolation via git worktree is an engineering detail that most agent frameworks overlook, yet it determines whether parallel tasks can actually run without corrupting each other's state.

Concepts & terms
function calling
The mechanism by which a language model outputs a structured request to invoke a tool, rather than free-form text. The engine parses this request, executes the corresponding function, and returns the result to the model as context.
context window
The fixed amount of text (conversation history plus tool results) a model can process in one request. When full, older information is lost or compressed, which degrades an agent's performance on long tasks.
git worktree
A Git feature that lets you check out multiple working copies of a repository at once, so parallel agents can edit separate copies without conflicting on the same files.
MCP (Model Context Protocol)
An open protocol for connecting external services (databases, browsers, GitHub) to an agent. Once configured, the service's tools appear as regular tools prefixed with mcp__ without modifying agent kernel code.
From the discussion
Featured comments
badhope

This post breaks down the Agent loop clearly. I've been adding something similar: every step in the loop must persist state, otherwise you have to replay from the start after an interruption. My state table now has run_id / step / tool_call_hash / result, and on resume I verify the hash before continuing. When you wrote your DeepSeek coding assistant, did you keep state in memory or in a database?

樊小肆

Persisted it, but I don't have a database. I write it directly into the trace.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗