Skip to content

Wrappers: add behaviour to any agent

A wrapper adds behaviour to an agent without writing a new agent: memory, a persona, extra instructions, or help with the reply format (SPEC-016). Wrappers work with every agent (hosted models, local models, scripted bots, your own classes), and you can stack several.

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

Built-in wrappers

Wrapper What it does Settings
memory Each turn, adds the agent's last turns to its notes: what it heard, the move it made, what it said, its reasoning, and why a move was rejected turns (default 10)
instructions Adds your text to every turn: a persona, a house rule, "talk more" text (required)
format_hints After a rejected reply, shows an example of a valid reply built from the legal moves, and reminds the model that messages go in say none

Models see these notes in their prompt; scripted agents ignore them, so the built-ins never break an agent.

Order

Wrappers run in the order you list them on the way in, and in reverse on the way out, like layers of an onion: the first one is the outermost.

Write your own

A wrapper has two optional hooks. before changes what the agent sees; after changes what it sends back. Save this as my_wrappers.py next to the scenario and use 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

    def after(self, request, reply):
        return reply
  • before may change observation, rules, notes, inbox and last_error, but not agent_id, step, actions or limits.
  • after may change the reply's action, say and reasoning, but must keep its usage, so cost limits keep working. The world still checks every move: a wrapper cannot make an illegal move legal.
  • Use reset(seed, agent_id) to clear per-game state, and the seed for any randomness.
  • An error in a wrapper counts as a bad reply from that agent, with the wrapper's name in the message.

In Python, wrap any agent with wrap(agent, [Memory(), FormatHints()]) from agentplaykit.core and agentplaykit.wrappers.

In the log, viewer and reports

  • run_start lists each agent's wrappers.
  • When wrappers changed what an agent saw, its observation event carries seen, and the viewer's "What x saw" panel shows it. A wrapper that changed a move is named in the action event.
  • In reports, a wrapped agent is a different competitor from the same model without wrappers: ollama:qwen2.5:3b+memory. Give it a name to choose the label yourself.

Comparing fairly

Wrapping only one side of a match changes what you compare: the result then says "this model with memory against that model without", not "this model against that model". To compare models, give both sides the same wrappers:

agents:
  - {id: qwen, use: ollama, config: {model: "qwen2.5:3b"}, wrap: [memory, format_hints]}
  - {id: llama, use: ollama, config: {model: "llama3.2:3b"}, wrap: [memory, format_hints]}

To measure what a wrapper does, run the same model with and without it against the same opponents, for example in a tournament with both as entrants.