Skip to content

Write an agent

An agent receives a Request each turn and returns a Reply. There are four ways to write one; pick whichever suits you.

The Request and the Reply

Each turn the agent gets plain JSON data:

{
  "agent_id": "x",
  "step": 4,
  "observation": {"board": [null, "x", null, "o", "x", null, null, null, "o"], "you": "x"},
  "actions": [0, 2, 5, 6, 7],
  "rules": "Tic-tac-toe on a 3x3 board...",
  "inbox": [{"from": "o", "to": "*", "text": "Nice one.", "step": 3}],
  "notes": [],
  "last_error": null,
  "limits": {"steps_left": 195, "seconds_left": 598.1, "cost_left_usd": 1.0}
}

and replies with at least an action:

{"action": 7, "say": [{"to": "o", "text": "Your move."}], "reasoning": "block the column"}

say sends messages (to is an agent ID, a list, or "*" for everyone). If an action is illegal, the next Request carries last_error and the agent tries again; three illegal moves in a row forfeit.

1. A Python class (under 30 lines)

--8 < --"examples/guess/bisect_agent.py"

Refer to it from a scenario by file name: use: bisect_agent.py. Use the seed given to reset(seed, agent_id) for any randomness, so runs can be reproduced.

2. A program in any language

The command adapter starts your program once per episode and talks JSON lines: one Request per line on stdin, one Reply per line on stdout.

--8 < --"examples/command-agent/my_agent.py"
# An agent written as a separate program (any language), talking JSON lines.
# Run from the repository root:
#   agentplaykit play examples/command-agent/scenario.yaml
agentplay: 1
name: command-agent
seed: 7
world: {use: tic-tac-toe}
agents:
  - id: x
    use: command
    config: {command: [python, examples/command-agent/my_agent.py], timeout_s: 10}
  - {id: o, use: scripted/random, config: {say: ["Good luck!", "Hmm."]}}

The program gets a minimal environment (no API keys) unless you list variables in pass_env.

3. A service behind a URL

- {id: x, use: http, config: {url: "http://localhost:8000/act"}}

AgentPlay POSTs the Request as JSON and reads the Reply. Redirects are never followed.

4. An MCP server

- {id: x, use: mcp, config: {command: [python, my_server.py], tool: act}}

The tool receives one argument, request, and returns the Reply as JSON text.

Test it

agentplaykit test examples/guess/bisect_agent.py --world examples/guess/guess_world.py

The kit plays your agent in a world and checks its replies, capabilities and determinism.

Or use a real model

You do not need to write code for model agents: see Real AI models.