Research: Agent Loop, Tool Calling, and Large File Handling in Crush #11
BlueHotDog
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Research: Agent Loop, Tool Calling, and Large File Handling in Crush
Date: 2025-10-03T07:19:56+0000
Researcher: Claude Code
Git Commit: 255aa3bcaaa79a270b48da607b2adab6a7e89bbd
Branch: main
Repository: crush
Research Question
How does this code deal with the need for an agent to read complex files, provide a deep dive analysis and research how the agent loop, tool calling and works in general and how it's coordinated for large
files?
Summary
The Crush codebase implements a goroutine-based agentic loop that coordinates iterative interactions between users, LLM providers, and tool execution.
The system handles complex file reading through the View tool with hard limits (250KB max file size, 2000 line default chunks) and manual pagination via offset/limit parameters. Tool calling follows a streaming event pattern where the LLM provider emits granular events (ToolUseStart, ToolUseDelta, ToolUseStop) that build tool calls incrementally, which are then executed sequentially with permission checks before results are appended to message history for the next iteration.
The architecture separates concerns across three main areas:
Detailed Findings
Agent Loop Architecture
Core Loop Location: /Users/danni/dev/crush/internal/llm/agent/agent.go:440-505
The agent implements an infinite for loop that continues until the LLM responds without tool calls. The execution flow:
Key Implementation Details:
Cancellation Pattern:
Tool Calling Mechanism
Tool Interface:
/Users/danni/dev/crush/internal/llm/tools/tools.go:69-73Registration Flow:
Streaming Tool Calls from Provider (anthropic.go:339-484):
The Anthropic provider (and others) emit events as the LLM streams its response:
Processing Events (agent.go:709-753):
Each event type updates the assistant message incrementally:
Execution Loop (agent.go:585-702):
After streaming completes, tools are executed sequentially (not concurrently):
Key Patterns:
File Reading for Large/Complex Files
Primary Implementation: /Users/danni/dev/crush/internal/llm/tools/view.go
Hard Limits:
MaxReadSize = 250 * 1024 // 250KB maximum file size
DefaultReadLimit = 2000 // Default lines per read
MaxLineLength = 2000 // Maximum characters per line
Reading Strategy (view.go:252-301):
The readTextFile() function uses a two-pass approach:
Pass 1 - Skip to Offset (lines 262-276):
Pass 2 - Read Requested Lines (lines 278-289):
Pass 3 - Count Remaining (lines 291-294):
Size Enforcement (view.go:177-181):
No Automatic Chunking Decision: The system does NOT analyze files and automatically decide when to chunk. Instead:
Special File Handling:
Output Formatting:
Integration Features:
Coordination Between Systems
Message as State Container: The Message object (/Users/danni/dev/crush/internal/message/message.go) serves as the primary state container with Parts []ContentPart holding heterogeneous content:
Data Flow for File Reading in Agent Loop:
Handling Large Files:
If a file has 10,000 lines:
File Size Rejection:
If file is 300KB:
Code References
Agent Loop
Tool Calling
File Reading
Supporting Infrastructure
Architecture Documentation
Agent Loop Design Principles
Tool Calling Design Principles
File Reading Design Principles
Coordination Patterns
Related Research
No existing research documents found in thoughts/ directory.
Open Questions
Beta Was this translation helpful? Give feedback.
All reactions