Coding Agent

This recipe builds a coding agent that explores a project, reads and writes files, and executes code to verify its work. It uses OpenAI as the model provider and opentine's filesystem and Python execution tools.

What It Does

  • Explores project structure with ls
  • Reads existing files to understand the codebase with read
  • Writes new files and edits existing ones with write
  • Executes Python code to test changes with execute
  • Saves the run tree so you can fork and retry if something goes wrong

Prerequisites

Terminal
export OPENAI_API_KEY="sk-..."

Full Example

coder.py
1from opentine import Agent
2from opentine.models.openai import OpenAI
3from opentine.tools.fs import read, write, ls
4from opentine.tools.python import execute
5
6agent = Agent(
7    model=OpenAI("gpt-4o"),
8    tools=[read, write, ls, execute],
9    system="""You are a senior Python developer. Given a task:
101. Explore the project structure with ls
112. Read relevant files to understand the codebase
123. Write or edit files to implement the solution
134. Execute code to verify your changes work""",
14    max_steps=50,
15)
16
17run = agent.run_sync("Add input validation to the user registration endpoint")
18print(f"Completed in {run.total_duration:.1f}s, cost {run.total_cost:.4f}")
19run.save("validation_feature.tine")

How It Works

The agent gets four tools: read, write, ls, and execute. The system prompt establishes a workflow — explore first, understand the code, make changes, then verify. The max_steps=50 limit gives it room for complex tasks while preventing runaway execution.

This pattern mirrors how a developer works: look at the project, read the relevant files, make changes, and run the tests.

Running It

Terminal
python coder.py

Example Run Output

Terminal
Run: validation_feature
Status: completed | Steps: 18 | Cost: $0.0523 | Duration: 34.2s

[think]  Understanding the task — need to add input validation...
[tool]   ls(".")
[tool]   ls("src/")
[tool]   read("src/routes/auth.py")
[tool]   read("src/models/user.py")
[model]  Found the registration endpoint, planning validation...
[tool]   write("src/validators/user.py")
[tool]   read("src/routes/auth.py")
[tool]   write("src/routes/auth.py")
[tool]   execute("python -m pytest tests/test_auth.py -v")
[done]   Validation added and tests passing

Forking to Fix a Failed Approach

If the agent's first approach didn't work — say the tests failed or the validation logic was wrong — fork from before the problematic step and try a different strategy. All the exploration and reading steps are reused.

fork_coder.py
1from opentine import Run, Agent
2from opentine.models.openai import OpenAI
3from opentine.tools.fs import read, write, ls
4from opentine.tools.python import execute
5
6# Load the run where tests failed
7run = Run.load("validation_feature.tine")
8
9# Fork from just before the write step to try a different approach
10forked = run.fork(from_step_id="step_before_write")
11
12agent = Agent(
13    model=OpenAI("gpt-4o"),
14    tools=[read, write, ls, execute],
15    system="Use pydantic for validation instead of manual checks.",
16)
17
18fixed = agent.resume(forked)
19fixed.save("validation_feature_v2.tine")

Adding Shell Access

For tasks that need shell commands (installing packages, running build tools, git operations), add the run tool alongside the filesystem tools.

coder_with_shell.py
1from opentine import Agent
2from opentine.models.openai import OpenAI
3from opentine.tools.fs import read, write, edit, ls
4from opentine.tools.shell import run
5from opentine.tools.python import execute
6
7agent = Agent(
8    model=OpenAI("gpt-4o"),
9    tools=[read, write, edit, ls, run, execute],
10    system="You are a coding assistant with shell access.",
11)
12
13result = agent.run_sync("Set up a new FastAPI project with a health endpoint")

Next Steps