Created
March 10, 2026 17:22
-
-
Save automata/68de7b48e314d5385e68fec1f08c3e01 to your computer and use it in GitHub Desktop.
Minimal Coding Agent in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import os | |
| import subprocess | |
| from pathlib import Path | |
| from openai import OpenAI | |
| MODEL = "anthropic/claude-opus-4-6" | |
| SYSTEM_PROMPT = ( | |
| "You are a precise coding assistant. " | |
| "Use tools when needed. Keep changes minimal and explain clearly." | |
| ) | |
| client = OpenAI( | |
| base_url="https://openrouter.ai/api/v1", | |
| api_key=os.environ["OPENROUTER_API_KEY"], | |
| ) | |
| tools = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "read", | |
| "description": "Read a UTF-8 text file", | |
| "parameters": { | |
| "type": "object", | |
| "properties": {"path": {"type": "string"}}, | |
| "required": ["path"], | |
| }, | |
| }, | |
| }, | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "write", | |
| "description": "Overwrite a UTF-8 text file", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "path": {"type": "string"}, | |
| "content": {"type": "string"}, | |
| }, | |
| "required": ["path", "content"], | |
| }, | |
| }, | |
| }, | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "edit", | |
| "description": "Replace exact text in a file", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "path": {"type": "string"}, | |
| "old_text": {"type": "string"}, | |
| "new_text": {"type": "string"}, | |
| }, | |
| "required": ["path", "old_text", "new_text"], | |
| }, | |
| }, | |
| }, | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "bash", | |
| "description": "Run a shell command in the current directory", | |
| "parameters": { | |
| "type": "object", | |
| "properties": {"command": {"type": "string"}}, | |
| "required": ["command"], | |
| }, | |
| }, | |
| }, | |
| ] | |
| def tool_read(path: str) -> str: | |
| return Path(path).read_text(encoding="utf-8") | |
| def tool_write(path: str, content: str) -> str: | |
| p = Path(path) | |
| p.parent.mkdir(parents=True, exist_ok=True) | |
| p.write_text(content, encoding="utf-8") | |
| return f"wrote {path}" | |
| def tool_edit(path: str, old_text: str, new_text: str) -> str: | |
| p = Path(path) | |
| original = p.read_text(encoding="utf-8") | |
| if old_text not in original: | |
| return "edit failed: old_text not found" | |
| p.write_text(original.replace(old_text, new_text, 1), encoding="utf-8") | |
| return f"edited {path}" | |
| def tool_bash(command: str) -> str: | |
| result = subprocess.run( | |
| command, | |
| shell=True, | |
| text=True, | |
| capture_output=True, | |
| timeout=30, | |
| ) | |
| output = (result.stdout or "") + (result.stderr or "") | |
| return output[:8000] or "(no output)" | |
| def execute_tool(name: str, args: dict) -> str: | |
| try: | |
| if name == "read": | |
| return tool_read(args["path"]) | |
| if name == "write": | |
| return tool_write(args["path"], args["content"]) | |
| if name == "edit": | |
| return tool_edit(args["path"], args["old_text"], args["new_text"]) | |
| if name == "bash": | |
| return tool_bash(args["command"]) | |
| return f"unknown tool: {name}" | |
| except Exception as e: | |
| return f"tool error: {e}" | |
| def run_agent(user_request: str): | |
| messages = [ | |
| {"role": "system", "content": SYSTEM_PROMPT}, | |
| {"role": "user", "content": user_request}, | |
| ] | |
| while True: | |
| response = client.chat.completions.create( | |
| model=MODEL, | |
| messages=messages, | |
| tools=tools, | |
| ) | |
| message = response.choices[0].message | |
| messages.append(message) | |
| if not message.tool_calls: | |
| print(f"\nassistant> {message.content}\n") | |
| return | |
| for tool_call in message.tool_calls: | |
| name = tool_call.function.name | |
| args = json.loads(tool_call.function.arguments or "{}") | |
| print(f"tool> {name}({args})") | |
| result = execute_tool(name, args) | |
| messages.append( | |
| { | |
| "role": "tool", | |
| "tool_call_id": tool_call.id, | |
| "content": result, | |
| } | |
| ) | |
| if __name__ == "__main__": | |
| YELLOW = "\033[93m" | |
| RESET = "\033[0m" | |
| print("My Coding Agent. Type 'exit' to quit.\n") | |
| while True: | |
| try: | |
| user_text = input(f"{YELLOW}agent>{RESET} ").strip() | |
| except (EOFError, KeyboardInterrupt): | |
| print("\nbye") | |
| break | |
| if not user_text: | |
| continue | |
| if user_text.lower() in {"exit", "quit"}: | |
| print("bye") | |
| break | |
| run_agent(user_text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment