Skip to content

SPEC-016: Agent wrappers

  • Status: Implemented
  • Milestone: v0.9 "Build your own"
  • Owners: lead maintainer

1. Purpose

Let users add behaviour to any agent (memory, a persona, extra instructions, help with the reply format) without writing a new agent class. Today each of these needs a subclass of one specific adapter, as the memory and "chatty" agents written for the Colab experiments did. A wrapper is a small reusable piece that sits between the runner and an agent: it may change what the agent sees before it acts, and what it sends back afterwards. Wrappers stack, work with every agent kind, and are recorded in the log, so comparisons stay honest.

2. Terms

Keywords MUST, SHOULD and MAY follow RFC 2119. - Wrapper: an object with before(request) and after(request, reply) hooks around one agent's act(). - Inner agent: the agent a wrapper stack wraps (any adapter or user agent, SPEC-002). - Stack: the ordered list of wrappers on one agent, written as wrap: in a scenario. - Seen request: the Request after every before hook ran, which is what the inner agent actually receives.

3. Requirements

Interface

  • WRP-001 core MUST provide a base class Wrapper with config, reset(seed, agent_id), before(request) -> request and after(request, reply) -> reply; both hooks default to returning their input unchanged. Like Agent, it lives in core and imports no adapter.
  • WRP-002 core MUST provide wrap(agent, wrappers) -> Agent, which returns an agent that runs the before hooks in list order, the inner agent's act(), then the after hooks in reverse order (the first wrapper is the outermost). The result MUST be usable anywhere an agent is (run_episode, scenarios, tournaments, judges).
  • WRP-003 A wrapped agent MUST keep the inner agent's identity: the same kind, model, capabilities and secrets. reset MUST reach the inner agent and every wrapper, so per-episode state (such as memory) starts empty each run.
  • WRP-004 Wrappers MUST receive only the Request and the Reply: never the world, the log, other agents or the runner (AGT-008, EVL-003). The runner MUST still check every action against the world, so a wrapper cannot make an illegal move legal.
  • WRP-005 A before hook MUST NOT change agent_id, step, actions or limits; it MAY change observation, rules, notes, inbox and last_error. A change to a forbidden field MUST raise an error naming the wrapper and the field.
  • WRP-006 An after hook MUST keep the inner reply's usage (so cost limits keep working) and MAY add to it when the wrapper made its own model calls; the runner sums them. It MAY change action, say and reasoning; any change to action MUST be recorded (WRP-009).
  • WRP-007 An exception raised by a wrapper MUST be reported as an AgentError whose message names the wrapper, so it counts like any agent failure (RUN-010) and never crashes the run.
  • WRP-008 Wrappers MUST be deterministic given the seed and their inputs; a wrapper that needs randomness MUST draw it from the seed passed to reset.

Scenarios and the log

  • WRP-009 An agent entry in a scenario MAY have wrap:, a list whose items are a wrapper name or a one-key map {name: config}. Names resolve like agents: a .py file next to the scenario, a local name, an installed plugin in the entry-point group agentplaykit.wrappers, or a built-in (SPEC-006). agentplaykit check MUST validate the names without importing plugin code (SCN-013).
  • WRP-010 run_start.agents[] MUST record the stack as wrap (names and redacted config). When wrappers changed the request, the observation event MUST also carry seen: the fields of the seen request that differ from the world's (for example notes or the observation). When an after hook changed the action, the action event MUST carry changed_by with the wrapper's name. The viewer's "What x saw" MUST show the seen request when there is one.
  • WRP-011 A competitor's key (RPT-002) MUST include its wrappers when it has any and no name, for example ollama:qwen2.5:3b+memory, so wrapped and unwrapped runs of the same model are rated separately. Reports MUST list each competitor's wrappers.

Built-in wrappers

  • WRP-012 The framework MUST ship memory: before each turn it adds to the request's notes one line, Your last turns: [...], with the agent's last turns turns (default 10): the step, the messages it received, its action, what it said, its reasoning, and why the action was rejected if it was. The observation keeps its shape, so agents that read it (scripted ones included) keep working.
  • WRP-013 The framework MUST ship instructions: it adds its text to the request's notes every turn, for personas ("You are a cautious player"), talking ("Send one short message to the other player every turn") or house rules.
  • WRP-014 The framework MUST ship format_hints: after a rejected reply (the request has a last_error), it adds to notes a short example of a valid reply built from the legal actions or their schema, for example {"action": 4} or {"action": "your argument as text"}. It changes nothing on turns without an error.
  • WRP-015 The built-in wrappers MUST work with every agent kind, including scripted ones, and MUST NOT call a model.

Docs and fairness

  • WRP-016 The docs MUST explain that wrapping only one side of a match changes what is being compared, and SHOULD show how to give every agent the same stack.

4. Acceptance criteria

Requirement Test ID Notes
WRP-001..008 WRP-001..WRP-008 order of hooks; identity; reset; forbidden fields; usage kept and summed; wrapper exceptions as agent errors; determinism
WRP-009..011 WRP-009..WRP-011 wrap: in scenarios with file, local, plugin and built-in names; check imports nothing; wrap, seen and changed_by in the log; report keys
WRP-012..015 WRP-012..WRP-015 each built-in against scripted agents and a fake model server; memory resets between runs
WRP-016 WRP-016 docs page check

5. Examples

agents:
  - id: qwen
    use: ollama
    config: {model: "qwen2.5:3b"}
    wrap:
      - memory: {turns: 6}
      - instructions: {text: "Send one short message to the other player every turn."}
      - format_hints
  - id: llama
    use: ollama
    config: {model: "llama3.2:3b"}
    wrap: [memory, format_hints]      # give both sides the same help

A wrapper of your own, in my_wrappers.py next to the scenario (wrap: [my_wrappers.py:Polite]):

from agentplaykit.core import Wrapper


class Polite(Wrapper):
    def before(self, request):
        request["notes"] = [*request["notes"], "Be polite to the other player."]
        return request

6. Open questions

  • Wrappers that call a model themselves (for example summarising a long memory): allowed by WRP-006, but no built-in does it yet.
  • Whether format_hints should become the default for small local models.

7. Change log

  • 2026-12-01: Initial draft.
  • 2026-12-01: Accepted by the lead maintainer for v0.9.
  • 2026-12-01: WRP-012: memory goes into notes, not the observation, found while building: changing the observation's shape broke scripted agents that read it (WRP-015).
  • 2026-12-03: Implemented in v0.9.0; every requirement has a test.

Changes to other specs on acceptance

  • SPEC-003, new LOG-025: run_start.agents[].wrap, observation.seen and action.changed_by (additive; log_version stays 1).
  • SPEC-005, new SCN-017: the wrap: key on agent entries and tournament entrants.
  • SPEC-006, new REG-012: the entry-point group agentplaykit.wrappers and built-in wrappers.
  • SPEC-013, RPT-002: competitor keys include wrappers (WRP-011).