中文 | English
How does Claude Code keep cycling between model output and tool execution until the task is done?
python examples/l2_agent_loop.pyThis requires DEEPSEEK_API_KEY.
query.tsQueryEngine.tstasks/
export async function* queryasync function* queryLoopwhile (true)tool_result
- who owns message history
- why tool results must be appended back into history
- what actually ends the loop
- how streamed events relate to the final answer
The demo shows the minimum loop. The real source also handles streaming events, recovery, cancellation, permissions, task state, and UI event emission.
- Why is the agent loop closer to a state machine plus event stream than a plain
whileloop? - If the model returns multiple tool calls, how does Claude Code keep the context coherent?
- What belongs in
QueryEngineand what belongs inquery.ts?