Overview
The agent loop is the core orchestrator in Rusty. It manages the conversation between the user, the LLM, and the tools.Loop Flow
Auto-compaction check
If the conversation exceeds approximately 80,000 tokens or 40 messages, older messages are summarized (keeping the last 10 messages) via an LLM call to reduce context window usage.
Send to LLM
The full message history, system prompt, and tool definitions are sent to the LLM provider via SSE streaming.
Stream response
The response is streamed back event by event: text deltas, thinking deltas, and tool call deltas are accumulated as they arrive.
Process tool calls
If the response contains tool calls, each tool is executed in order. Results are appended to the message history as tool result messages.
Cancellation
The agent loop can be cancelled at any time viaCtrl+C in TUI mode. Cancellation sets an atomic flag that the loop checks between steps. When cancelled:
- The current LLM stream is terminated
- Partial text is discarded
- The session state is preserved (no messages are lost)
Turn Limits
The--max-turns flag limits the number of agent loop iterations. Each iteration where tool calls are executed counts as one turn. This prevents runaway loops where the agent keeps calling tools indefinitely.
System Prompt
The system prompt is assembled from several sources:- Tool descriptions: Names, descriptions, and input schemas for all available tools
- Permission mode: Current permission mode and allowed tools
- Platform info: Operating system, architecture, working directory
- Git status: Current branch, recent commits, working tree status
- Sandbox notice: Reminder that file operations are restricted to the working directory
- Context files: Contents of AGENTS.md, CLAUDE.md, or RUSTY.md files found in the project
- Current date: For time-sensitive operations
- Plan-with-tasks instructions: Optional instructions for structured task tracking
Streaming
All LLM communication is streaming-first. The provider yields a stream of events:| Event | Description |
|---|---|
TextDelta | A chunk of response text |
ThinkingDelta | A chunk of reasoning/thinking content |
ToolCallDelta | A chunk of a tool call (name or arguments) |
Usage | Token usage information |
Done | Stream completed |
Error | An error occurred |
Callbacks
The agent accepts optional callbacks for real-time UI updates:| Callback | Purpose |
|---|---|
TextCallback | Called with each text delta |
ThinkingCallback | Called with each thinking delta |
ToolCallback | Called when a tool call starts or completes |
PermissionCallback | Called when a tool requires permission |
Sub-Agents
Sub-agents are spawned as independent Tokio tasks with their own agent loop instance. They run withBypassPermissions and have access to all tools except the agent tool (to prevent recursive spawning). The parent agent receives the sub-agent’s final response as a single text block.