Part 10: Agent Loop Engineering

Part 9 established why an Agent needs a harness. Part 10 examines the runtime mechanisms that keep that harness useful under long contexts, large tool surfaces, restarts, mid-run user input, hung providers, repeated actions, and parallel calls.

Public-source snapshot: implementation details are examples from public Kocoro origin/main at commit 4ec6772, reviewed 2026-07-27. Treat the invariant as the lesson and re-check constants against current source.

Chapter map

ChapterTopicQuestion answered
34From DAG to Agent LoopExplains when a deterministic DAG should give way to an adaptive Agent loop, and what state a long-lived daemon must rebuild for every turn.
35Context CompactionDesigns a staged context-compaction ladder that acts before overflow, verifies summaries, and retains an emergency recovery path.
36Tool Result Budget and SpillShows how context offloading stores large tool results outside the prompt while preserving a useful preview and durable pointer.
37Tiered CompressionExplains semantic compaction that preserves recent tool evidence and progressively reduces older results by age and value.
38Deferred Tool Loading and Tool SearchReduces MCP tool-schema bloat with token-budget and category-based deferral, a warm set, and model-visible tool search.
39Prompt Cache StabilityShows how stable prompt prefixes, byte-equal forked requests, and execution-time permission checks improve Agent prompt-cache reuse.
40Durable Agent LoopsDesigns resumable Agent turns with phase-boundary checkpoints, interrupted markers, bounded unattended recovery, and idempotent persistence.
41Steering a Running AgentExplains how running Agents absorb follow-ups, preserve message addressing, distinguish delivery acknowledgement from replay, and interrupt safely.
42Agent Timeouts and WatchdogsSeparates soft-idle warnings, hard cancellation, stream-gap detection, and phase-aware watchdog policy for hung Agents.
43Stuck Loop DetectionBuilds stuck-loop detection from complementary progress signals, bounded nudges, force-stop behavior, and a public zero-byte write incident.
44Parallel Tool ExecutionExplains safe parallel tool calls using per-invocation concurrency traits, ordered result pairing, and conservative Bash classification.
45Computer-Use Context ManagementSeparates text observations, browser screenshots, and global image budgets so GUI loops retain actionable state without unbounded prompt growth.

Reading paths

  • Context and cost: 35 → 36 → 37 → 38 → 39
  • Durability and control: 40 → 41 → 42
  • Reliability, speed, and GUI context: 43 → 44 → 45
  • Coming from DAG workflows: 34 → 40 → 43 → 44

Source boundary

Only tracked files on Kocoro public origin/main are used. Local-only architecture notes, unpushed branches, private postmortems, hosted-service topology, internal tokens, and private repository paths are excluded.

Chapter 45 is included because its observation-window and image-budget implementation is now tracked on public origin/main.

Cite this article
Zhang, Wayland (2026). Part 10: Agent Loop Engineering. In AI Agent Architecture: From Single Agent to Enterprise Multi-Agent Systems. https://waylandz.com/ai-agent-book-en/part10-agent-loop-engineering/
@incollection{zhang2026aiagent_en_part10-agent-loop-engineering,
  author = {Zhang, Wayland},
  title = {Part 10: Agent Loop Engineering},
  booktitle = {AI Agent Architecture: From Single Agent to Enterprise Multi-Agent Systems},
  year = {2026},
  url = {https://waylandz.com/ai-agent-book-en/part10-agent-loop-engineering/}
}