File size: 4,806 Bytes
a6f3c54 cd815be |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
---
license: agpl-3.0
language:
- en
base_model:
- Qwen/Qwen3-Coder-30B-A3B-Instruct
---
# Coding agent
A simple coding agent built with Qwen_Distilled_Coder (via OpenRouter) that can view/edit files and execute bash commands—all in ~200 lines.
```mermaid
flowchart TD
Start([Start]) --> UserInput[Get User Input]
UserInput --> Qwen_Distilled_Coder[Send to Qwen_Distilled_Coder]
Qwen_Distilled_Coder --> NeedsTools{Needs Tools?}
NeedsTools -->|No| ShowResponse[Show Response]
NeedsTools -->|Yes| ExecuteTools[Execute Tools]
ExecuteTools --> SendResults[Send Results to Qwen_Distilled_Coder]
SendResults --> Qwen_Distilled_Coder
ShowResponse --> UserInput
ExecuteTools -.-> Tools
```
## Quick start
1. **Create virtual environment and install dependencies**:
```bash
# Option 1: uv installed
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv sync
# Option 2: Without uv
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip3 install uv
uv sync
```
2. **Setup environment & add API key**:
```bash
cp .env.example .env
```
Be sure to add your OpenRouter API key! You can get one from [OpenRouter](https://openrouter.ai/keys).
3. **Run the CLI agent**:
```bash
uv run simple_agent.py
```
Note: `uv` and an appropriate virtualenv are prerequisites—our agent will use uv to execute Python scripts
## Using OpenRouter with Qwen Distilled Coder
This agent uses the [OpenRouter](https://openrouter.ai/) API to access the Qwen_Distilled_Coder model. OpenRouter provides a unified API for accessing various AI models, including those from Anthropic, OpenAI, and others.
To use this agent:
1. Sign up for an account at [OpenRouter](https://openrouter.ai/)
2. Create an API key at [OpenRouter Keys](https://openrouter.ai/keys)
3. Add your API key to the `.env` file
4. Run the agent with `uv run simple_agent.py`
The agent uses the OpenAI client library but points it to the OpenRouter API endpoint, allowing it to access the Qwen_Distilled_Coder model.
## What it does
- **Fix broken files**: `"can you help me fix broken_file.py?"`
- **Research and implement**: `"research new Python 3.13 features and write a file that demonstrates a simple example"`
- **Create new code**: `"write a simple tip splitting calculator Python file"`
## Architecture
The agent follows a straightforward pattern with three core components:
### Prompt structure
```xml
<role>
You are an expert software engineering assistant...
</role>
<thinking_process>
Before taking any action, think through the problem step by step...
</thinking_process>
<instructions>
When working with code:
1. Understanding First: Always examine existing files...
2. Targeted Changes: Use precise `str_replace` operations...
</instructions>
```
**Best practices:**
- Split system prompt (role) from user instructions for better caching
- Use XML tags for structured prompts and interpretability
- Include chain-of-thought reasoning with `<thinking_process>` blocks
- Cache tools, system prompt, and first user message for cost optimization
### Tool execution router
```python
def execute_tool(tool_name: str, tool_input: dict) -> dict:
"""Execute a tool and return structured result with error handling."""
try:
if tool_name == "view":
# Handle file/directory viewing
elif tool_name == "str_replace":
# Handle targeted file edits
elif tool_name == "bash":
# Handle command execution with timeout
# ...
except Exception as e:
return {"content": f"Error: {str(e)}", "is_error": True}
```
**Best practices:**
- Return structured responses with `is_error` flag for Qwen_Distilled_Coder
- Use proper timeout protection (30s default for bash)
- Include detailed error logging and handling
- Support both file operations and bash commands
### Agent loop
```python
while True:
response = client.messages.create(
model=ANTHROPIC_MODEL,
system=[{"type": "text", "text": system_prompt}],
messages=messages,
tools=ANTHROPIC_TOOLS,
)
if response.stop_reason == "tool_use":
# Execute tools in parallel when possible
# Return results to Qwen_Distilled_Coder for continued processing
else:
# Handle final response
break
```
**Best practices:**
- Handle all stop reasons robustly (tool_use, end_turn, etc.)
- Execute multiple tools in parallel when possible
- Maintain conversation state through message history
- Use low temperature (0.2) for consistent, focused responses
## Files
- `simple_agent.py` - CLI version
- `prompt.md` - System prompt and instructions
## Requirements
- Python 3.13+
- OpenRouter API key
|