One LLM Contract¶
Each agent in a Mash host picks its own model in build_llm(), so a cheap model can handle triage while another agent runs on a capable one, even across vendors in the same deployment. The runtime supports this by keeping provider SDKs behind the adapter layer: everything above it speaks exactly two types, LLMRequest in and LLMResponse out.
# src/mash/core/llm/types.py: what the runtime sends
LLMRequest(
model: str,
system: SystemPrompt,
messages: list[LLMMessage],
tools: list[LLMToolDefinition],
max_tokens: int,
temperature: float = 1.0,
use_prompt_caching: bool = True,
streaming: bool = False,
provider_options: dict[str, Any] = {},
)
The response side mirrors it: text, parsed tool_calls, normalized content_blocks, a normalized stop_reason, and LLMTokenUsage with cache read/write counts. The raw SDK response is preserved in provider_response for debugging. This post covers what the adapters do to honor that contract, since the three providers disagree on most of the details.
Three wire formats, one translation layer¶
flowchart LR
R["LLMRequest\n(normalized)"] --> A["AnthropicProvider\nā Messages API"]
R --> O["OpenAIProvider\nā Responses API"]
R --> G["GeminiProvider\nā Interactions API"]
A --> N["LLMResponse\n(normalized)"]
O --> N
G --> N
The differences the adapters absorb:
| Anthropic | OpenAI | Gemini | |
|---|---|---|---|
| API | Messages | Responses | Interactions |
| Prompt caching | cache_control breakpoints on system/tool blocks |
prompt_cache_key + retention |
implicit prefix caching, nothing to configure |
| Streaming | yes | yes | yes |
| Structured output | output_config json_schema |
text.format json_schema |
response_format json_schema |
| Quirks | beta flags via provider_options |
temperature omitted for gpt-5* |
schema types coerced to "OBJECT" uppercase |
Prompt caching is where the gap is widest. The developer-facing surface is one boolean, prompt_caching_enabled in AgentConfig, on by default, which flows into the request as use_prompt_caching. What happens next differs completely per vendor. The Anthropic adapter annotates system and tool blocks with cache breakpoints. The OpenAI adapter attaches a cache key with configurable retention. The Gemini adapter does nothing, because the Interactions API it speaks caches implicitly: Google matches the leading tokens of each request against recent ones and discounts the prefix it recognizes, so use_prompt_caching has no knob to map onto and the adapter ignores it. All of it stays inside the adapter, so an agent spec that switches AnthropicProvider for GeminiProvider changes one line.
Streaming without a second contract¶
send() stays the single generation entry point with the same return type whether or not the response streams.
When request.streaming is set and the adapter supports it, the provider streams internally. As text arrives it emits coalesced chat.delta events, the frames you saw riding the SSE stream in the durable loop post, and then returns the fully accumulated LLMResponse, identical in shape to the non-streaming case. Chunks are flushed by size or interval so event volume stays at tens of events per turn rather than hundreds. The request's chat.end remains the source of truth for duration and token counts; deltas are a progress channel.
An adapter without streaming support ignores the hint and returns the same response shape. The answer just arrives all at once.
Structured output, per provider¶
The structured-output flow from the runtime (finalize_structured_output, the second LLM call after a run completes) drives providers through provider_options["structured_output"], a JSON schema dict. Each adapter detects the key and translates it to its native schema-enforcement feature, listed in the table above. The instruction asking the model to produce the payload is added by the runtime; the adapters enforce shape. The result is that structured_output=MyPydanticModel on a request behaves the same against all three vendors.
provider_options itself is the escape hatch for vendor-specific settings, such as Anthropic beta flags or Gemini thinking levels. Anything that needs to work everywhere gets promoted to a real LLMRequest field; the rest stays in the dict.
What the contract buys the rest of the system¶
Two consequences show up elsewhere in the series.
First, the durable loop rebuilds context from its event log and replays earlier turns from it, which is possible because messages and content blocks are normalized types with stable shapes. coerce_content_blocks even normalizes legacy forms (tool_use ā tool_call) so stored context from older runs keeps deserializing.
Second, observability gets provider-uniform events for free. The runtime records every model call the same way whichever adapter made it: chat.start and chat.end around the call, with the provider, model and token counts as GenAI attributes on chat.end, so the trace analysis at the end of this series, and any OpenTelemetry backend it exports to, sees uniform fields across vendors.
The same two types absorb more than three vendors. An open-source model served over a Chat Completions endpoint plugs into the identical LLMRequest/LLMResponse contract through a fourth adapter, so an agent can run on Qwen or Llama with the same one-line change in build_llm().
Next: Open-Source Models over Chat Completions.