For a complete implementation, see the Python Guide. For TypeScript SDK wrappers, see Tool.
Message Flow
The agent runs a multi-turn conversation with max 4 turns:| Turn | Role | Content |
|---|---|---|
| 0 | system | System prompt with tool definitions |
| 1 | user | Repo structure + search query |
| 2 | assistant | Agent’s tool calls |
| 3 | user | Tool execution results |
| 4+ | … | Repeat until finish is called |
Initial User Message
The first user message contains two parts:- Repository structure — pre-run
list_directoryat root with depth 2 - Search query — what the agent needs to find
API Call
Agent Response Format
The agent first thinks, then outputs tool calls using nested XML:Tools
- grep
- read
- list_directory
- finish
Search for regex pattern matches using ripgrep.Agent calls:
You execute:You return:Output format: Match lines use
| Element | Required | Description |
|---|---|---|
<pattern> | Yes | Regex pattern to search |
<sub_dir> | No | Directory to search (default .) |
<glob> | No | File filter like *.py or *.{ts,tsx} |
: separator (filepath:linenum:content), context lines use - separator (filepath-linenum-content), groups separated by --.Result Format
After executing tools, wrap each result in XML tags that echo the query:Turn Counter
After tool results, append a turn counter and context budget:| Turn | Message |
|---|---|
| 1 | You have used 0 turns and have 4 remaining |
| 2 | You have used 1 turn and have 3 remaining |
| 3 | You have used 2 turns and have 2 remaining |
| 4 | You have used 3 turns, you only have 1 turn remaining. You have run out of turns to explore the code base and MUST call the finish tool now |
Output Limits
Tools enforce output limits to prevent context explosion:| Tool | Max Lines | On Exceed |
|---|---|---|
grep | 200 | Replace with “query not specific enough, tool called tried to return too much context and failed” |
list_directory | 200 | Same |
read | 800 | Truncate with warning |
Complete Example
Here’s the full message flow for a 2-turn search:Turn 1: Initial request
Turn 1: Initial request
User message:Assistant response:
Turn 2: Tool results + finish
Turn 2: Tool results + finish
User message (tool results):Assistant response:
System Prompt
View full system prompt
View full system prompt
Implementation Guide
For a complete working implementation, see the Python Guide which covers:- API call function
- XML parsing for tool calls
- Tool executors (ripgrep, file reads, tree)
- Result formatters
- Complete agent loop