CH 02 · The Agent Loop · 09/11
Read as one pageRead the source yourself
Four stops, smallest first. Each is the loop you built, with one more real problem solved.
- mini-swe-agent,
src/minisweagent/agents/default.py. Read the file straight through. Then openinteractive.pynext to it and find which methodInteractiveAgentoverrides to ask you before a command runs. - pi,
packages/agent/src/agent-loop.ts. Trace one prompt throughrunLoopon paper: write downhasMoreToolCallsandpendingMessagesafter every lap of a two-tool-call task. - pi,
packages/agent/src/agent.ts. Find wheregetSteeringMessagesandgetFollowUpMessagesare wired to the two queues. Then readcontinue(): what happens to queued messages when the last message in the history is the assistant's? - Codex,
codex-rs/core/src/session/turn.rs. Intry_run_sampling_request, list every line that setsneeds_follow_upto true. Expect tool calls and a denied or directly answered tool request (both arrive throughoutput_result.needs_follow_up), an unfinishedend_turn, and a few stream interruptions.
Exercise for you: give your agent_loop two upgrades from this chapter. First, make finishing a tool: add a submit tool, and treat a plain-text reply as an error you append to history and send back (mini-swe-agent's rule). Second, when fake_model returns "stop_reason": "length", append an error result for each tool call instead of running it (pi's rule). Script fake_model to hit both cases once and check that the loop recovers.
Finished this idea?
Your learning progress stays in this browser.