Skip to content

Commit 8176550

Browse files
committed
merge agent-guide.md into .agents/readme
1 parent 50cc5a9 commit 8176550

File tree

2 files changed

+238
-212
lines changed

2 files changed

+238
-212
lines changed

common/src/templates/initial-agents-dir/README.md

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@ Create specialized agent workflows that coordinate multiple AI agents to tackle
1010

1111
## Need Help?
1212

13-
- For detailed documentation, see [agent-guide.md](./agent-guide.md).
1413
- For examples, check the `examples/` directory.
1514
- Join our [Discord community](https://codebuff.com/discord) and ask your questions!
15+
- Check our [documentation](https://codebuff.com/docs) for more details
16+
17+
# What is Codebuff?
18+
19+
Codebuff is an **open-source AI coding assistant** that edits your codebase through natural language instructions. Instead of using one model for everything, it coordinates specialized agents that work together to understand your project and make precise changes.
20+
21+
Codebuff beats Claude Code at 61% vs 53% on [our evals](https://github.com/CodebuffAI/codebuff/tree/main/evals) across 175+ coding tasks over multiple open-source repos that simulate real-world tasks.
22+
23+
## How Codebuff Works
24+
25+
When you ask Codebuff to "add authentication to my API," it might invoke:
26+
27+
1. A **File Explorer Agent** to scan your codebase to understand the architecture and find relevant files
28+
2. A **Planner Agent** to plan which files need changes and in what order
29+
3. An **Editor Agent** to make precise edits
30+
4. A **Reviewer Agent** to validate changes
31+
32+
This multi-agent approach gives you better context understanding, more accurate edits, and fewer errors compared to single-model tools.
1633

1734
## Context Window Management
1835

@@ -54,3 +71,223 @@ export default {
5471
```
5572

5673
This agent systematically analyzes changes, reads relevant files for context, then creates commits with clear, meaningful messages that explain the "why" behind changes.
74+
75+
# Agent Development Guide
76+
77+
This guide covers everything you need to know about building custom Codebuff agents.
78+
79+
## Agent Structure
80+
81+
Each agent is a TypeScript file that exports an `AgentDefinition` object:
82+
83+
```typescript
84+
export default {
85+
id: 'my-agent', // Unique identifier (lowercase, hyphens only)
86+
displayName: 'My Agent', // Human-readable name
87+
model: 'claude-3-5-sonnet', // AI model to use
88+
toolNames: ['read_files', 'write_file'], // Available tools
89+
instructionsPrompt: 'You are...', // Agent behavior instructions
90+
spawnerPrompt: 'Use this agent when...', // When others should spawn this
91+
spawnableAgents: ['helper-agent'], // Agents this can spawn
92+
93+
// Optional: Programmatic control
94+
async *handleSteps() {
95+
yield { tool: 'read_files', paths: ['src/config.ts'] }
96+
yield 'STEP' // Let AI process and respond
97+
},
98+
}
99+
```
100+
101+
## Core Properties
102+
103+
### Required Fields
104+
105+
- **`id`**: Unique identifier using lowercase letters and hyphens only
106+
- **`displayName`**: Human-readable name shown in UI
107+
- **`model`**: AI model from OpenRouter (see [available models](https://openrouter.ai/models))
108+
- **`instructionsPrompt`**: Detailed instructions defining the agent's role and behavior
109+
110+
### Optional Fields
111+
112+
- **`toolNames`**: Array of tools the agent can use (defaults to common tools)
113+
- **`spawnerPrompt`**: Instructions for when other agents should spawn this one
114+
- **`spawnableAgents`**: Array of agent names this agent can spawn
115+
- **`handleSteps`**: Generator function for programmatic control
116+
117+
## Available Tools
118+
119+
### File Operations
120+
121+
- **`read_files`**: Read file contents
122+
- **`write_file`**: Create or modify entire files
123+
- **`str_replace`**: Make targeted string replacements
124+
- **`code_search`**: Search for patterns across the codebase
125+
126+
### Execution
127+
128+
- **`run_terminal_command`**: Execute shell commands
129+
- **`spawn_agents`**: Delegate tasks to other agents
130+
- **`end_turn`**: Finish the agent's response
131+
132+
### Web & Research
133+
134+
- **`web_search`**: Search the internet for information
135+
- **`read_docs`**: Read technical documentation
136+
- **`browser_logs`**: Navigate and inspect web pages
137+
138+
See `types/tools.ts` for detailed parameter information.
139+
140+
## Programmatic Control
141+
142+
Use the `handleSteps` generator function to mix AI reasoning with programmatic logic:
143+
144+
```typescript
145+
async *handleSteps() {
146+
// Execute a tool
147+
yield { tool: 'read_files', paths: ['package.json'] }
148+
149+
// Let AI process results and respond
150+
yield 'STEP'
151+
152+
// Conditional logic
153+
if (needsMoreAnalysis) {
154+
yield { tool: 'spawn_agents', agents: ['deep-analyzer'] }
155+
yield 'STEP_ALL' // Wait for spawned agents to complete
156+
}
157+
158+
// Final AI response
159+
yield 'STEP'
160+
}
161+
```
162+
163+
### Control Commands
164+
165+
- **`'STEP'`**: Let AI process and respond once
166+
- **`'STEP_ALL'`**: Let AI continue until completion
167+
- **Tool calls**: `{ tool: 'tool_name', ...params }`
168+
169+
## Model Selection
170+
171+
Choose models based on your agent's needs:
172+
173+
- **`anthropic/claude-sonnet-4`**: Best for complex reasoning and code generation
174+
- **`openai/gpt-5`**: Strong general-purpose capabilities
175+
- **`x-ai/grok-4-fast`**: Fast and cost-effective for simple or medium-complexity tasks
176+
177+
**Any model on OpenRouter**: Unlike Claude Code which locks you into Anthropic's models, Codebuff supports any model available on [OpenRouter](https://openrouter.ai/models) - from Claude and GPT to specialized models like Qwen, DeepSeek, and others. Switch models for different tasks or use the latest releases without waiting for platform updates.
178+
179+
See [OpenRouter](https://openrouter.ai/models) for all available models and pricing.
180+
181+
## Agent Coordination
182+
183+
Agents can spawn other agents to create sophisticated workflows:
184+
185+
```typescript
186+
// Parent agent spawns specialists
187+
async *handleSteps() {
188+
yield { tool: 'spawn_agents', agents: [
189+
'security-scanner',
190+
'performance-analyzer',
191+
'code-reviewer'
192+
]}
193+
yield 'STEP_ALL' // Wait for all to complete
194+
195+
// Synthesize results
196+
yield 'STEP'
197+
}
198+
```
199+
200+
**Reuse any published agent**: Compose existing [published agents](https://www.codebuff.com/store) to get a leg up. Codebuff agents are the new MCP!
201+
202+
## Best Practices
203+
204+
### Instructions
205+
206+
- Be specific about the agent's role and expertise
207+
- Include examples of good outputs
208+
- Specify when the agent should ask for clarification
209+
- Define the agent's limitations
210+
211+
### Tool Usage
212+
213+
- Start with file exploration tools (`read_files`, `code_search`)
214+
- Use `str_replace` for targeted edits, `write_file` for major changes
215+
- Always use `end_turn` to finish responses cleanly
216+
217+
### Error Handling
218+
219+
- Include error checking in programmatic flows
220+
- Provide fallback strategies for failed operations
221+
- Log important decisions for debugging
222+
223+
### Performance
224+
225+
- Choose appropriate models for the task complexity
226+
- Minimize unnecessary tool calls
227+
- Use spawnable agents for parallel processing
228+
229+
## Testing Your Agent
230+
231+
1. **Local Testing**: `codebuff --agent your-agent-name`
232+
2. **Debug Mode**: Add logging to your `handleSteps` function
233+
3. **Unit Testing**: Test individual functions in isolation
234+
4. **Integration Testing**: Test agent coordination workflows
235+
236+
## Publishing & Sharing
237+
238+
1. **Validate**: Ensure your agent works across different codebases
239+
2. **Document**: Include clear usage instructions
240+
3. **Publish**: `codebuff publish your-agent-name`
241+
4. **Maintain**: Update as models and tools evolve
242+
243+
## Advanced Patterns
244+
245+
### Conditional Workflows
246+
247+
```typescript
248+
async *handleSteps() {
249+
const config = yield { tool: 'read_files', paths: ['config.json'] }
250+
yield 'STEP'
251+
252+
if (config.includes('typescript')) {
253+
yield { tool: 'spawn_agents', agents: ['typescript-expert'] }
254+
} else {
255+
yield { tool: 'spawn_agents', agents: ['javascript-expert'] }
256+
}
257+
yield 'STEP_ALL'
258+
}
259+
```
260+
261+
### Iterative Refinement
262+
263+
```typescript
264+
async *handleSteps() {
265+
for (let attempt = 0; attempt < 3; attempt++) {
266+
yield { tool: 'run_terminal_command', command: 'npm test' }
267+
yield 'STEP'
268+
269+
if (allTestsPass) break
270+
271+
yield { tool: 'spawn_agents', agents: ['test-fixer'] }
272+
yield 'STEP_ALL'
273+
}
274+
}
275+
```
276+
277+
## Why Choose Codebuff for Custom Agents
278+
279+
**Deep customizability**: Create sophisticated agent workflows with TypeScript generators that mix AI generation with programmatic control. Define custom agents that spawn subagents, implement conditional logic, and orchestrate complex multi-step processes that adapt to your specific use cases.
280+
281+
**Fully customizable SDK**: Build Codebuff's capabilities directly into your applications with a complete TypeScript SDK. Create custom tools, integrate with your CI/CD pipeline, build AI-powered development environments, or embed intelligent coding assistance into your products.
282+
283+
Learn more about the SDK [here](https://www.npmjs.com/package/@codebuff/sdk).
284+
285+
## Community & Support
286+
287+
- **Discord**: [Join our community](https://codebuff.com/discord) for help and inspiration
288+
- **Examples**: Study the `examples/` directory for patterns
289+
- **Documentation**: [codebuff.com/docs](https://codebuff.com/docs) and check `types/` for detailed type information
290+
- **Issues**: [Report bugs and request features on GitHub](https://github.com/CodebuffAI/codebuff/issues)
291+
- **Support**: [[email protected]](mailto:[email protected])
292+
293+
Happy agent building! 🤖

0 commit comments

Comments
 (0)