Skip to content

SPEC-009: Adapters

  • Status: Implemented
  • Milestone: M4; presets in v0.2.0 (ADR-0005)
  • Owners: lead maintainer

1. Purpose

AgentPlay is model-neutral: any AI agent can play, hosted or local, from any vendor or none. Adapters connect agents to the Agent interface (SPEC-002) and add usage, cost and key handling. No provider is privileged by the core; specific providers are ordinary adapters that anyone can add.

2. Terms

  • Adapter: an agent plugin that connects some kind of agent. Generic adapter: works with any agent speaking a common protocol. Provider adapter: wraps one vendor's SDK.
  • Local agent: a model or program running on the user's machine or network, needing no cloud account.
  • Fixture: a recorded provider response used offline in tests.
  • Preset: a built-in adapter for one OpenAI-compatible service, defined as a row of defaults over openai_compat (ADR-0005). Hosted preset: a preset for a cloud service that needs an API key.

3. Requirements

Neutrality

  • ADP-001 The core MUST NOT import, name, special-case or default to any model vendor. Adapters are selected only by the scenario use reference (SPEC-005).
  • ADP-002 Every adapter MUST implement the same Agent interface and receive the same Request; the same scenario MUST run with any adapter by changing only the agent's use line and config.
  • ADP-003 All adapters MUST tell the agent the rules, legal actions and reply format identically, so comparisons between models are fair.

Built-in adapters

  • ADP-004 The framework MUST ship these generic adapters, which need no vendor SDK:
  • openai_compat: any OpenAI-compatible chat endpoint via base_url, such as LM Studio, vLLM and llama.cpp, or any service without a preset. Implemented over plain HTTP (standard library).
  • the provider presets of ADP-020, built on openai_compat.
  • command: runs any local program, in any language, speaking JSON over stdin and stdout: one Request line in, one Reply line out. Wraps local CLIs and models, or custom agents.
  • http: POSTs the Request as JSON to any URL and reads the Reply, for agents served by other processes or machines.
  • scripted/random and scripted/minimax: deterministic from the seed, free (ADP-013).
  • ADP-005 The framework MUST ship these SDK-based adapters as optional extras, each one equal to any third-party adapter: anthropic (agentplaykit[anthropic]) and mcp (agentplaykit[mcp]). Further vendors whose API is not OpenAI-compatible (for example Bedrock) SHOULD be added as separate plugins through the agentplaykit.agents entry point (REG-002); OpenAI-compatible services MAY instead be added as presets (ADP-020).
  • ADP-006 A user MUST be able to write their own agent as a Python class or function (SPEC-002, AGT-011) and use it with no framework changes.

Packaging

  • ADP-007 Provider SDKs MUST be imported only inside their own adapter module and installed as optional extras; a missing extra MUST produce an error naming the extra to install. Core and the generic adapters MUST work with no extras installed.

Config, capabilities, cost

  • ADP-008 Adapter config MUST accept, where meaningful: model, base_url, api_key_env (env var name; optional, since local servers often need no key), temperature, max_output_tokens, timeout_s, retries, command / url.
  • ADP-009 Adapters MUST declare capabilities (AGT-006) and use native tool calls where supported, otherwise plain-text prompting with a strict parser; the log records which mode was used. Small local models without tool calls MUST work through the plain-text mode.
  • ADP-010 Replies MUST report usage and meta.provider / meta.model as the endpoint returns them (AGT-004, AGT-005). If an endpoint reports no token counts, input_tokens and output_tokens MAY be estimated and the estimate MUST be flagged "estimated": true.
  • ADP-011 Cost MUST be computed from an explicit price in config: {input_per_mtok, output_per_mtok} in USD per million tokens. v0.1 ships no built-in price table, because prices change and must never be guessed. A cost reported by the provider is used when no price is set (ADP-022). Endpoints on the local machine (localhost, 127.0.0.1, ::1, 0.0.0.0) default to cost_usd: 0; command agents and http agents report their own usage or cost 0. Any other agent without a price reports cost_usd: null, and the runner applies the token cap instead (RUN-019).
  • ADP-017 openai_compat and every preset MUST default to plain-text mode (works with every server) and use native tool calls only when config sets tool_calls: true; json_mode: true MAY request a JSON response format.
  • ADP-018 The anthropic adapter MUST let the server choose tool use (tool_choice: auto), since some models reject forced tool choice, and MUST NOT send sampling parameters unless config sets them. A refusal stop reason MUST be reported as an AgentError naming the stop details.
  • ADP-019 The mcp adapter MUST start the configured MCP server over stdio, call the configured tool (default act) with one argument, request, holding the Request, and parse the tool's text result as a Reply. It requires Python 3.10 or newer (the MCP SDK's minimum).

Provider presets (ADR-0005)

  • ADP-020 The framework MUST ship these presets as built-in adapters, each a row in one table over openai_compat:
use default base_url default api_key_env
openai https://api.openai.com/v1 OPENAI_API_KEY
openrouter https://openrouter.ai/api/v1 OPENROUTER_API_KEY
groq https://api.groq.com/openai/v1 GROQ_API_KEY
deepseek https://api.deepseek.com/v1 DEEPSEEK_API_KEY
gemini https://generativelanguage.googleapis.com/v1beta/openai GEMINI_API_KEY
mistral https://api.mistral.ai/v1 MISTRAL_API_KEY
together https://api.together.xyz/v1 TOGETHER_API_KEY
ollama http://localhost:11434/v1 none

A preset only fills in missing config: any key, including base_url and api_key_env, MAY be overridden, and api_key_env: null sends no key. model stays required. Replies MUST record the preset name as meta.provider unless config sets provider. A hosted preset MUST NOT count as a local endpoint for cost (ADP-011), even when base_url points at the local machine (for example a proxy), unless config sets local: true. - ADP-021 A hosted preset MUST check its key when the agent is created: if the variable is not set, it MUST raise a config error naming the variable and the service, before any request is sent. The suggested fix MUST say how to send no key (api_key_env: null), since removing api_key_env restores the default. - ADP-022 The openrouter preset MUST ask the service to report usage ("usage": {"include": true}) and, when config sets no price, MUST use the cost the response reports (usage.cost, USD) as cost_usd. A missing, non-numeric, negative or non-finite reported cost MUST be ignored, and ADP-011 applies. - ADP-023 The openrouter preset MUST NOT send attribution headers by default; config app_url sends HTTP-Referer and app_name sends X-Title. - ADP-024 The ollama preset MUST need no key, cost 0 by default (ADP-011), and when the server cannot be reached (connection refused, or host not found) its error MUST name the fix: start Ollama with ollama serve. A timeout is reported as a timeout, since a running Ollama can be slow to load a model. - ADP-012 Retries MUST use exponential backoff with jitter and honor Retry-After; each attempt MUST be reported (AGT-010). Local connection failures (server not running) MUST give an error with the fix (for example "is the server at http://localhost:11434 running?"). - ADP-013 Scripted bots MUST be deterministic from the seed and cost nothing.

Security and testing

  • ADP-014 API keys MUST be read from the environment only, and MUST NOT be logged or appear in error text (SEC-001, SEC-002).
  • ADP-015 The command adapter runs user-chosen programs; it MUST NOT use a shell by default, MUST pass only the Request on stdin, MUST enforce timeout_s, MUST NOT give the program the log or world objects (AGT-008), and MUST start it with the minimal environment of SEC-010.
  • ADP-016 Every adapter and preset MUST ship recorded fixtures or a local test double, so CI needs no keys, network or model; live tests are opt-in via an environment flag.

4. Acceptance criteria

Requirement Test ID Notes
ADP-001 ADP-001 static check: no vendor names in core/
ADP-002..003 ADP-002, ADP-003 same scenario, swap use; identical prompts
ADP-004..006 ADP-004..ADP-006 local fake HTTP server and fake command program, no SDK installed
ADP-007 ADP-007 install without extras; import boundary check
ADP-008..011 ADP-008..ADP-011 recorded OpenAI-compatible and Anthropic responses; no-token-count fallback
ADP-012..013 ADP-012, ADP-013 429 fixture, server-down message
ADP-014..016 ADP-014..ADP-016 fake key never leaks; command timeout
ADP-020..024 ADP-020..ADP-024 each preset against a local fake server with base_url overridden; OpenRouter cost fixture

5. Examples

agents:
  - {id: a, use: scripted/random}                                                  # free bot
  - {id: b, use: openai_compat, config: {base_url: "http://localhost:11434/v1", model: llama3}}  # local
  - {id: c, use: openrouter, config: {model: some/model}}                         # key from OPENROUTER_API_KEY
  - {id: f, use: ollama, config: {model: llama3.2}}                               # local, free
  - {id: g, use: openai_compat, config: {base_url: "http://localhost:1234/v1", model: local-model}}  # any other server
  - {id: d, use: command, config: {command: ["python", "my_agent.py"]}}            # any program
  - {id: e, use: anthropic, config: {model: some-model, api_key_env: ANTHROPIC_API_KEY}}

6. Open questions

  • Image observations for vision models: deferred.
  • A native Ollama (non-OpenAI-compatible) adapter: only if the OpenAI-compatible mode proves insufficient.

7. Change log

  • 2026-11-30: Initial draft.
  • 2026-11-30: Made model-neutral: generic openai_compat, command and http adapters, Anthropic as one optional adapter, local-agent rules.
  • 2026-11-30: Accepted by the lead maintainer.
  • 2026-11-30: Resolved the price-table question (user-supplied price, local endpoints free, no guessed prices); added ADP-017 to ADP-019.
  • 2026-11-30: ADP-019 passes the Request as a single request argument, since MCP tools declare fixed parameters.
  • 2026-11-30: ADP-015 refers to SEC-010 (minimal environment for agent programs).
  • 2026-11-30: Implemented in v0.1.0; every requirement has a test (PRO-004, PRO-011).
  • 2026-11-30: Provider presets (ADR-0005): ADP-020 to ADP-024 added; ADP-004, ADP-005, ADP-011, ADP-016 and ADP-017 updated for presets.
  • 2026-11-30: Review of the presets: hosted presets are never local for cost unless local: true (ADP-020), the missing-key fix names api_key_env: null (ADP-021), reported cost is validated (ADP-022), and the Ollama hint covers unknown hosts (ADP-024).
  • 2026-11-30: ADP-020 to ADP-024 implemented in v0.2.0.