This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathv1_basic_agent.py
More file actions
422 lines (340 loc) · 13.2 KB
/
v1_basic_agent.py
File metadata and controls
422 lines (340 loc) · 13.2 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
"""
v1_basic_agent.py - Mini Claude Code: Model as Agent (~200 lines)
Core Philosophy: "The Model IS the Agent"
=========================================
The secret of Claude Code, Cursor Agent, Codex CLI? There is no secret.
Strip away the CLI polish, progress bars, permission systems. What remains
is surprisingly simple: a LOOP that lets the model call tools until done.
Traditional Assistant:
User -> Model -> Text Response
Agent System:
User -> Model -> [Tool -> Result]* -> Response
^________|
The asterisk (*) matters! The model calls tools REPEATEDLY until it decides
the task is complete. This transforms a chatbot into an autonomous agent.
KEY INSIGHT: The model is the decision-maker. Code just provides tools and
runs the loop. The model decides:
- Which tools to call
- In what order
- When to stop
The Four Essential Tools:
------------------------
Claude Code has ~20 tools. But these 4 cover 90% of use cases:
| Tool | Purpose | Example |
|------------|----------------------|----------------------------|
| bash | Run any command | npm install, git status |
| read_file | Read file contents | View src/index.ts |
| write_file | Create/overwrite | Create README.md |
| edit_file | Surgical changes | Replace a function |
With just these 4 tools, the model can:
- Explore codebases (bash: find, grep, ls)
- Understand code (read_file)
- Make changes (write_file, edit_file)
- Run anything (bash: python, npm, make)
Usage:
python v1_basic_agent.py
"""
import os
import subprocess
import sys
from pathlib import Path
from dotenv import load_dotenv
# Load configuration from .env file
load_dotenv()
try:
from anthropic import Anthropic
except ImportError:
sys.exit("Please install: pip install anthropic python-dotenv")
# =============================================================================
# Configuration
# =============================================================================
API_KEY = os.getenv("ANTHROPIC_API_KEY")
BASE_URL = os.getenv("ANTHROPIC_BASE_URL")
MODEL = os.getenv("MODEL_NAME", "claude-sonnet-4-20250514")
WORKDIR = Path.cwd()
# Initialize client - handles both direct Anthropic and compatible APIs
client = Anthropic(api_key=API_KEY, base_url=BASE_URL) if BASE_URL else Anthropic(api_key=API_KEY)
# =============================================================================
# System Prompt - The only "configuration" the model needs
# =============================================================================
SYSTEM = f"""You are a coding agent at {WORKDIR}.
Loop: think briefly -> use tools -> report results.
Rules:
- Prefer tools over prose. Act, don't just explain.
- Never invent file paths. Use bash ls/find first if unsure.
- Make minimal changes. Don't over-engineer.
- After finishing, summarize what changed."""
# =============================================================================
# Tool Definitions - 4 tools cover 90% of coding tasks
# =============================================================================
TOOLS = [
# Tool 1: Bash - The gateway to everything
# Can run any command: git, npm, python, curl, etc.
{
"name": "bash",
"description": "Run a shell command. Use for: ls, find, grep, git, npm, python, etc.",
"input_schema": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The shell command to execute"
}
},
"required": ["command"],
},
},
# Tool 2: Read File - For understanding existing code
# Returns file content with optional line limit for large files
{
"name": "read_file",
"description": "Read file contents. Returns UTF-8 text.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Relative path to the file"
},
"limit": {
"type": "integer",
"description": "Max lines to read (default: all)"
},
},
"required": ["path"],
},
},
# Tool 3: Write File - For creating new files or complete rewrites
# Creates parent directories automatically
{
"name": "write_file",
"description": "Write content to a file. Creates parent directories if needed.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Relative path for the file"
},
"content": {
"type": "string",
"description": "Content to write"
},
},
"required": ["path", "content"],
},
},
# Tool 4: Edit File - For surgical changes to existing code
# Uses exact string matching for precise edits
{
"name": "edit_file",
"description": "Replace exact text in a file. Use for surgical edits.",
"input_schema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Relative path to the file"
},
"old_text": {
"type": "string",
"description": "Exact text to find (must match precisely)"
},
"new_text": {
"type": "string",
"description": "Replacement text"
},
},
"required": ["path", "old_text", "new_text"],
},
},
]
# =============================================================================
# Tool Implementations
# =============================================================================
def safe_path(p: str) -> Path:
"""
Ensure path stays within workspace (security measure).
Prevents the model from accessing files outside the project directory.
Resolves relative paths and checks they don't escape via '../'.
"""
path = (WORKDIR / p).resolve()
if not path.is_relative_to(WORKDIR):
raise ValueError(f"Path escapes workspace: {p}")
return path
def run_bash(command: str) -> str:
"""
Execute shell command with safety checks.
Security: Blocks obviously dangerous commands.
Timeout: 60 seconds to prevent hanging.
Output: Truncated to 50KB to prevent context overflow.
"""
# Basic safety - block dangerous patterns
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
if any(d in command for d in dangerous):
return "Error: Dangerous command blocked"
try:
result = subprocess.run(
command,
shell=True,
cwd=WORKDIR,
capture_output=True,
text=True,
timeout=60
)
output = (result.stdout + result.stderr).strip()
return output[:50000] if output else "(no output)"
except subprocess.TimeoutExpired:
return "Error: Command timed out (60s)"
except Exception as e:
return f"Error: {e}"
def run_read(path: str, limit: int = None) -> str:
"""
Read file contents with optional line limit.
For large files, use limit to read just the first N lines.
Output truncated to 50KB to prevent context overflow.
"""
try:
text = safe_path(path).read_text()
lines = text.splitlines()
if limit and limit < len(lines):
lines = lines[:limit]
lines.append(f"... ({len(text.splitlines()) - limit} more lines)")
return "\n".join(lines)[:50000]
except Exception as e:
return f"Error: {e}"
def run_write(path: str, content: str) -> str:
"""
Write content to file, creating parent directories if needed.
This is for complete file creation/overwrite.
For partial edits, use edit_file instead.
"""
try:
fp = safe_path(path)
fp.parent.mkdir(parents=True, exist_ok=True)
fp.write_text(content)
return f"Wrote {len(content)} bytes to {path}"
except Exception as e:
return f"Error: {e}"
def run_edit(path: str, old_text: str, new_text: str) -> str:
"""
Replace exact text in a file (surgical edit).
Uses exact string matching - the old_text must appear verbatim.
Only replaces the first occurrence to prevent accidental mass changes.
"""
try:
fp = safe_path(path)
content = fp.read_text()
if old_text not in content:
return f"Error: Text not found in {path}"
# Replace only first occurrence for safety
new_content = content.replace(old_text, new_text, 1)
fp.write_text(new_content)
return f"Edited {path}"
except Exception as e:
return f"Error: {e}"
def execute_tool(name: str, args: dict) -> str:
"""
Dispatch tool call to the appropriate implementation.
This is the bridge between the model's tool calls and actual execution.
Each tool returns a string result that goes back to the model.
"""
if name == "bash":
return run_bash(args["command"])
if name == "read_file":
return run_read(args["path"], args.get("limit"))
if name == "write_file":
return run_write(args["path"], args["content"])
if name == "edit_file":
return run_edit(args["path"], args["old_text"], args["new_text"])
return f"Unknown tool: {name}"
# =============================================================================
# The Agent Loop - This is the CORE of everything
# =============================================================================
def agent_loop(messages: list) -> list:
"""
The complete agent in one function.
This is the pattern that ALL coding agents share:
while True:
response = model(messages, tools)
if no tool calls: return
execute tools, append results, continue
The model controls the loop:
- Keeps calling tools until stop_reason != "tool_use"
- Results become context (fed back as "user" messages)
- Memory is automatic (messages list accumulates history)
Why this works:
1. Model decides which tools, in what order, when to stop
2. Tool results provide feedback for next decision
3. Conversation history maintains context across turns
"""
while True:
# Step 1: Call the model
response = client.messages.create(
model=MODEL,
system=SYSTEM,
messages=messages,
tools=TOOLS,
max_tokens=8000,
)
# Step 2: Collect any tool calls and print text output
tool_calls = []
for block in response.content:
if hasattr(block, "text"):
print(block.text)
if block.type == "tool_use":
tool_calls.append(block)
# Step 3: If no tool calls, task is complete
if response.stop_reason != "tool_use":
messages.append({"role": "assistant", "content": response.content})
return messages
# Step 4: Execute each tool and collect results
results = []
for tc in tool_calls:
# Display what's being executed
print(f"\n> {tc.name}: {tc.input}")
# Execute and show result preview
output = execute_tool(tc.name, tc.input)
preview = output[:200] + "..." if len(output) > 200 else output
print(f" {preview}")
# Collect result for the model
results.append({
"type": "tool_result",
"tool_use_id": tc.id,
"content": output,
})
# Step 5: Append to conversation and continue
# Note: We append assistant's response, then user's tool results
# This maintains the alternating user/assistant pattern
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": results})
# =============================================================================
# Main REPL
# =============================================================================
def main():
"""
Simple Read-Eval-Print Loop for interactive use.
The history list maintains conversation context across turns,
allowing multi-turn conversations with memory.
"""
print(f"Mini Claude Code v1 - {WORKDIR}")
print("Type 'exit' to quit.\n")
history = []
while True:
try:
user_input = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
break
if not user_input or user_input.lower() in ("exit", "quit", "q"):
break
# Add user message to history
history.append({"role": "user", "content": user_input})
try:
# Run the agent loop
agent_loop(history)
except Exception as e:
print(f"Error: {e}")
print() # Blank line between turns
if __name__ == "__main__":
main()