A simple coding agent, implemented in Python.
README.md

Agenty: a simple coding agent #

This is the code for a simple coding agent named agenty. It is based on the coding agent in https://github.com/ghuntley/how-to-build-a-coding-agent.

This agent is written in Python (not Go). It uses uv for project and dependency management.

It is to be built in increments.

Build steps #

1. Basic Chat — talk to the LLM #

Port chat.go to chat.py.

2. File Reader — read code files #

Port read.go to read.py.

3. Permissions — decide what a tool may do #

No Go original for this one. Add an allow/ask/deny layer that every tool call passes through before it runs. It goes here, before the shell tool, on purpose: once a command runner exists, anything read_file refuses can be read with cat, so guards inside a tool protect nothing. The gate has to sit above the tools. Modelled on OpenCode's permission config — see the discussion below.

4. File Explorer — list files in folders #

Port list_files.go to list_files.py.

5. Command Runner — run shell commands #

Port bash_tool.go to bash_tool.py.

6. File Editor — modify files #

Port edit_tool.go to edit_tool.py.

7. Code Search — search your codebase with patterns #

Port code_search_tool.go to code_search_tool.py.

Tools #

Step 2 introduces the tool loop, which steps 4–7 extend. Each tool is a ToolDefinition — name, description, JSON schema, and a Python callable — held in TOOLS in chat.py, which grows one entry per step.

Agent.run sends that list with every request. When the model replies with tool calls, the agent runs each one, appends the results, and asks again, repeating until the model answers in prose. One user turn can therefore become several round trips.

Two details are easy to get wrong:

  • A tool turn legitimately has no prose: content is None and finish_reason is "tool_calls". That is a success, not the truncation IncompleteResponse rejects.
  • The assistant turn carrying tool_calls goes back into the conversation before the tool results, or the gateway will not accept them.

Tool failures never raise. tools.invoke turns a bad argument, a missing file or an unknown tool into text handed back to the model, which can then correct itself — the same reason read.go reports errors rather than aborting.

--no-tools disables the lot.

read_file confines reads to the working directory #

read.go reads whatever path it is handed. That is fine for a tool you invoke yourself, but this one is driven by text from a model, which can be talked into asking for ../../etc/passwd. read.py resolves paths against the working directory and refuses absolute paths or anything that escapes it. This is a deliberate deviation from the Go original — remove it if you want the permissive behaviour.

This is a guard in one tool, not a security boundary. Step 5 gives the model a shell, and a shell can read anything, so step 3 has to exist before it.

Permissions #

Step 3 replaces tool-local guards with a single gate that every tool call passes through. Modelled on OpenCode's permission config, which is a good reference because it solves the same problem with the same constraints.

The shape, briefly:

  • Each tool call resolves to allow, ask or deny before it runs.
  • Rules match on the tool's input, not just its name: a file path for read_file, a command line for the shell tool.
  • Last matching rule wins, so broad rules go first and exceptions after.
  • ask offers once, always (the tool suggests the pattern it would whitelist) or reject.
  • Defaults are permissive with deliberate carve-outs. OpenCode allows reads but denies *.env, re-allowing *.env.example; it asks before any path reaches outside the working directory.

Two things worth copying deliberately:

  • The gate is above the tools, not inside them. A tool never decides whether it may run. That is what keeps the shell tool from being a way around every check a file tool does.
  • Defaults protect people who configure nothing. Worth having even though this project has one user.

Two things OpenCode notably does not do, so decide them independently rather than inheriting them: no cap on how much a read returns (a multi-megabyte file goes straight into the context and the bill), and no redaction of secrets inside files it did permit reading.

The honest scope of any of this: the model asks, the harness decides. It does not constrain a determined model, it ensures anything outside the stated defaults needs a human to say yes.

Build details #

The application is built using Red-Green-Refactor TDD. Unit tests go into a test/unit subdirectory. We use the Python unittest module for testing.

uv run python -m unittest discover -s test -t .
uv run ruff check .

ruff is a development dependency ([dependency-groups] dev in pyproject.toml); install it with uv sync.

Unit tests (test/unit) replace litellm.completion and cover the chat loop and configuration. Integration tests (test/integration) point the real litellm client at a local HTTP server (test/integration/fake_gateway.py) and assert on the actual request. That second tier exists because mocking completion cannot see the wire, and the URL that gets built from a model prefix is exactly where this goes wrong.

Live gateway checks are opt-in and skipped by default:

AGENTY_LIVE=1 OPENCODE_API_KEY=... uv run python -m unittest discover -s test/integration -t .

The LLM API #

Rather than the Anthropic API used by the original, agenty calls LiteLLM against OpenCode Go or Zen. One OpenCode key from https://opencode.ai/auth covers both.

These are not plain OpenAI-compatible endpoints. Each model is served on one of three path suffixes, and LiteLLM encodes the choice in the model string prefix:

Endpoint Prefix Used by
/chat/completions openai/ Kimi, DeepSeek, GLM, MiniMax, LongCat, MiMo, Hy, Space Bunny
/responses openai/responses/ Grok, GPT Luna, Muse Spark
/messages anthropic/ Claude, Qwen

Sending the wrong suffix fails loudly rather than degrading, so the mapping in src/agenty/config.py is a routing decision, not decoration. Set AGENT_PROVIDER to go or zen to choose the gateway.

Two things about this mapping are easy to get wrong, and both are pinned by tests:

  • LiteLLM builds the path itself, and not uniformly. The OpenAI routes append /chat/completions or /responses to whatever base you give them, but the Anthropic route appends /v1/messages. A base ending in /v1 — which is what every OpenCode URL is — would therefore produce /v1/v1/messages for Messages models. config.api_base() strips the /v1 for those models only.
  • Go and Zen can disagree for the same model. The docs list one endpoint per model, but minimax-m3 serves /messages on Go and only /chat/completions on Zen. Checked against the live gateways, since GET /models returns no protocol information. Such cases live in _PROTOCOL_OVERRIDES.
Variable Default Purpose
OPENCODE_API_KEY — required
AGENT_PROVIDER go go or zen
AGENT_MODEL deepseek-v4.1-flash see MODELS in config.py
AGENT_MAX_TOKENS 1024 reply ceiling; --max-tokens overrides

Run it with uv run agenty, or uv run agenty --once "prompt" for a single turn, and --verbose to send diagnostics to stderr. ctrl-c exits with status 130 and no traceback.

Notes:

  • OpenCode Go requires the x-opencode-session header and rejects requests without it, despite the docs describing it only as recommended. agenty sends a per-process session id, which is also what Go uses for prompt caching.
  • Go bills reasoning tokens against max_tokens, so a reply can be cut off partway through, or be all reasoning with no content. Either way chat.py raises IncompleteResponse and keeps the turn out of the conversation — a truncated assistant turn would otherwise be replayed on every later turn, so the model would treat its own unfinished sentence as something it said. Whatever text did come back is printed first; the message names the ceiling in force and how to raise it (--max-tokens / AGENT_MAX_TOKENS).
  • Some Go models require Global regions: set Privacy to Global in your workspace settings at https://opencode.ai/auth, or use --provider zen. deepseek-v4.1-flash is one of these.