Inside a Homebuilt DeepSeek Coding Agent: It's Just a while(true) Loop
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.
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.
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.
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.