Skip to content

Instantly share code, notes, and snippets.

@mbutler
Created June 25, 2026 15:07
Show Gist options
  • Select an option

  • Save mbutler/c0f478ff7aae26438e6b8f65503fbafd to your computer and use it in GitHub Desktop.

Select an option

Save mbutler/c0f478ff7aae26438e6b8f65503fbafd to your computer and use it in GitHub Desktop.
A minimal harness for agents
import subprocess
import re
# 1. Initialize Memory
messages = [
{"role": "system", "content": "You are an agent. Write code in ```python filename.py format. I will run it and tell you the result."}
]
max_iterations = 3
current_iteration = 0
while current_iteration < max_iterations:
# 2. Call the Model (e.g., OpenAI, Anthropic, or local model API)
response = call_llm(messages)
messages.append({"role": "assistant", "content": response})
# 3. Parse and Write File
code_blocks = extract_code_and_filenames(response) # simple regex
for filename, code in code_blocks:
with open(filename, "w") as f:
f.write(code)
# 4. Execute and Capture Feedback (The Deterministic Loop)
result = subprocess.run(["python", "test_target.py"], capture_output=True, text=True)
if result.returncode == 0:
print("Success! Tests passed.")
break # Exit the loop, task complete
else:
# 5. Feed the error back into the model's memory
error_message = f"Execution failed:\n{result.stderr}\nPlease fix the code."
messages.append({"role": "user", "content": error_message})
current_iteration += 1
if current_iteration == max_iterations:
print("Agent failed to solve the task within iteration limit.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment