DEV Community

neuzhou
neuzhou

Posted on

every AI agent I've read has a god object. after 12 codebases I think I know why.

I've spent the last few months reading through AI agent source code. Not the docs -- the actual implementations. 12 projects so far: Claude Code, Cline, Dify, Goose, Codex CLI, DeerFlow, and six others.

Every single one has a god object.

Not like "oh this file is a bit big." I mean a single class or module that handles the agent loop, streaming, tool execution, context management, error recovery, and half a dozen other concerns that have no business being in the same file. Cline's is 3,756 lines. Hermes Agent's is 9,000. Claude Code's query.ts is 1,729 lines and it's actually one of the smaller ones.

At first I thought this was just organic code growth -- ship fast, refactor later, except later never comes. But after seeing the same pattern in 12 completely unrelated projects built by different teams, I started thinking it might be something deeper.

Here's what I think is going on.

An agent loop is a state machine. Every iteration reads context, calls a model, parses tool calls, executes tools, handles results, and decides whether to continue. These six steps share a huge amount of mutable state: the conversation history, streaming buffers, tool results, checkpoint data, permission state, hook lifecycle.

The moment you try to extract one step into its own class, you discover it needs access to the state from three other steps. So you either pass around a massive context object (which is just a god object with extra indirection) or you give up and keep everything together.

The while-loop architecture makes this almost inevitable. 4 out of 12 projects I read use a literal while(true) as their core loop. The rest use variations that amount to the same thing. And a while-loop with shared mutable state across iterations will always converge toward a single class that owns all of it.

The one project that avoids this is Dify. They use a DAG (directed acyclic graph) instead of a while-loop. Each step is a node, data flows through edges, nodes are isolated. No god object. But the cost is 7+ containers, 400+ environment variables, and 11 config files just to run locally. They traded one problem for another.

Nobody else has found a middle path. You either get the fast-to-build while-loop with its inevitable god object, or you get the clean-but-complex graph architecture. 12 codebases and zero exceptions.

I don't have a solution. I'm just reporting what I found. If someone has seen an agent architecture that avoids both the god object and the container sprawl, I genuinely want to know about it.

Full teardowns for all 12 projects with architecture diagrams and line-by-line references: awesome-ai-anatomy

Top comments (0)