Files
infrapuzzle/docs/adr/0002-openclaw-model-selection-tiering-and-economics.md
T

25 KiB
Raw Blame History

status, date, decision-makers, consulted, informed
status date decision-makers consulted informed
accepted (fleet-wide primary: DeepSeek-V4.1-Flash-1017; updated 2026-10-17) 2026-09-13
Moritz Graf
Antigravity

ADR 0002: OpenClaw Full-Spectrum Model Selection, Multimodal Tiering, and Cost Governance

Context and Problem Statement

OpenClaw serves as the 24/7 personal AI concierge, family assistant, and operational orchestrator for Moritz Graf and his family. It interacts across Telegram, voice notes, webhooks, and background automations, bridging to personal knowledge (Obsidian wiki), smart home systems (Home Assistant, Sonos), fitness tracking (Garmin Connect), and cluster monitoring (Prometheus, Gitea).

During early September 2026, OpenClaw consumed 143.26 million tokens over 7 days, accumulating $12.55+ in raw OpenRouter API costs. Forensic analysis revealed that over 80% of this spend was architectural waste:

  1. Unbounded Context Re-transmission: An uncompacted session (agent:main:main) reached 381k tokens (2,650 events, 5.7 MB JSON). Every minor turn re-ingested this entire history, costing $12.20 (97.2% of total spend).
  2. Heartbeat Amplification Loop: Background heartbeats firing every 30 minutes targeted the active user session (target: last), re-evaluating the 381k context 48 times/day simply to output NO_REPLY ($5.16 wasted).
  3. Expensive & Deprecated Model Allocations: Frontier models (claude-3.7-sonnet and deepseek-v4-pro) were configured without clear cost-benefit boundaries, with subagents frequently timing out (>300s). In addition, legacy model generations (e.g., Gemini 2.5) lingered in configuration despite superior, newer generations being available.
  4. Modality Conflation (Image Input vs. Image Generation): Vision perception (analyzing an uploaded photo) was conflated with text-to-image synthesis (drawing an image). Configuring chat models like z-ai/glm-5.3-flash for image synthesis caused runtime HTTP 404 failures because pure LLMs lack diffusion/raster generation heads.

Moritz established two foundational requirements for the repository:

  1. "I want high quality, but it should not be too expensive."
  2. "We always want to use the newest models (as they usually are the best)."

How do we architect the full-spectrum model selection across all modalities (text, subagents, heartbeats, vision, image generation, audio transcription, text-to-speech, web search, and vector embeddings) to deliver premium quality, sub-10s latency, and a total operating budget of <$2.00 / week?


Decision Drivers

  • State-of-the-Art Recency: Exclusively employ the latest generation models (e.g. DeepSeek V4.1 Flash 1017, Gemini 3.8/3.1, GLM 5.3, Whisper V3 Turbo, Sonar Pro). Completely decommission legacy generations (e.g. Gemini 2.5).
  • Conversational & Reasoning Quality: High emotional intelligence, nuanced German fluency, personal context recall, and strict adherence to tool-calling protocols (ADR 0001).
  • Strict Modality Decoupling: Each capability must route to its dedicated, best-in-class engine:
    • Text & Agentic Reasoning
    • Native Multimodal Vision (reading user photos)
    • Text-to-Image Generation (synthesizing new pictures)
    • Speech-to-Text / Audio Input (transcribing voice messages)
    • Text-to-Speech / Audio Output (synthesizing briefings and podcasts)
    • Web Search & Retrieval (grounded search)
    • Semantic Embeddings (vector memory)
  • Inference Economics: Low per-token costs for large conversational contexts (15k–80k tokens), high prompt-cache discount efficiency (cacheRead), zero background bleed, and predictable billing.
  • Latency & Turn Responsiveness: Conversational turns on Telegram must return within 4–10 seconds, avoiding provider queuing or massive prefill bottlenecks.
  • Resilience & Fault Tolerance: Automated fallback chains and strict quantization/provider pins (fp8/bf16, tool_choice support) across independent providers on OpenRouter.

Full-Spectrum Model Architecture & Concrete Allocations

┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                       OpenClaw Ingress Gateway                                         │
└──────────────────────────────────────────────────┬─────────────────────────────────────────────────────┘
                                                   │
         ┌───────────────────┬─────────────────────┼─────────────────────┬───────────────────┐
         ▼                   ▼                     ▼                     ▼                   ▼
┌─────────────────┐ ┌─────────────────┐   ┌─────────────────┐   ┌─────────────────┐ ┌─────────────────┐
│ Text Conductor  │ │ Subagent Engine │   │ Background &    │   │ Image Synthesis │ │ Voice & Audio   │
│ & Vision Input  │ │ (sessions_spawn)│   │ Heartbeat       │   │ (Text-to-Image) │ │ (STT & TTS)     │
├─────────────────┤ ├─────────────────┤   ├─────────────────┤   ├─────────────────┤ ├─────────────────┤
│ Tier 1          │ │ Tier 2          │   │ Tier 3          │   │ Tier 4          │ │ Tier 5          │
│ • Low Latency   │ │ • Deep Analysis │   │ • target: none  │   │ • Nano Banana 2 │ │ • Whisper V3    │
│ • Tool Routing  │ │ • Autonomous    │   │ • Zero Bleed    │   │ • Dedicated     │ │ • Gemini 3.1    │
│ • Decoupled OCR │ │ • Isolated Run  │   │ • 30m Polling   │   │   Raster Head   │ │   Flash TTS     │
└────────┬────────┘ └────────┬────────┘   └────────┬────────┘   └────────┬────────┘ └────────┬────────┘
         │                   │                     │                     │                   │
         ▼                   ▼                     ▼                     ▼                   ▼
┌─────────────────┐ ┌─────────────────┐   ┌─────────────────┐   ┌─────────────────┐ ┌─────────────────┐
│ Primary:        │ │ Primary:        │   │ Primary:        │   │ Primary:        │ │ STT:            │
│ deepseek/       │ │ deepseek/       │   │ deepseek/       │   │ google/         │ │ openai/         │
│ deepseek-v4.1-  │ │ deepseek-v4.1-  │   │ deepseek-v4.1-  │   │ gemini-3.1-     │ │ whisper-large-  │
│ flash-1017      │ │ flash-1017      │   │ flash-1017      │   │ flash-image     │ │ v3-turbo        │
│                 │ │                 │   │                 │   │                 │                   │
│ Fallbacks:      │ │ Fallbacks:      │   │ Fallbacks:      │   │ Fallbacks:      │ │ TTS:            │
│ 1. v4.1-flash   │ │ 1. v4.1-flash   │   │ 1. v4.1-flash   │   │ 1. gemini-3.1-  │ │ google/         │
│ 2. glm-5.3-flash│ │ 2. glm-5.3-flash│   │ 2. glm-5.3-flash│   │    flash-preview│ │ gemini-3.1-     │
│ 3. qwen3.8-flash│ │ 3. qwen3.8-flash│   │ 3. qwen3.8-flash│   │ 2. gemini-3-pro-│ │ flash-tts       │
│                 │ │                 │   │                 │   │    image        │ │ (FB: MiniMax 2.8│
│ Vision (OCR):   │ │                 │   │                 │   │                 │ │  Speech HD)     │
│ google/         │ │                 │   │                 │   │                 │ │                 │
│ gemini-3.8-     │ │                 │   │                 │   │                 │ │                 │
│ flash           │ │                 │   │                 │   │                 │ │                 │
└─────────────────┘ └─────────────────┘   └─────────────────┘   └─────────────────┘ └─────────────────┘

Modality Specifications & Cost-Benefit Analysis

1. Interactive Conductor & Agentic Reasoning (Tier 1)

  • Primary (Fleet-Wide Standard as of 2026-10-17): openrouter/deepseek/deepseek-v4.1-flash-1017
    • 1017 Snapshot Upgrade (Pareto Jump in Quality & Price): Released on October 17, 2026, deepseek/deepseek-v4.1-flash-1017 boosts the Artificial Analysis Intelligence Index from 24.7 to 39.5 (+60%) via Reinforcement Learning with Self-Feedback (RLSF) while cutting DeepInfra fp8 pricing by an additional 30% ($0.098/M prompt, $0.294/M completion, and $0.00294/M cacheRead vs. $0.140/$0.420/$0.0042 on the 0910 baseline).
    • Provider Pinning & Tool-Compatibility Verification:
      • Pinned order: ["deepinfra", "cloudflare", "inference-net", "fireworks", "parasail"], quantizations: ["fp8", "bf16", "unknown"], and ignore: ["morph", "baseten", "deepseek", "open-inference", "decart", "sail-research", "wafer"].
      • Cloudflare (#2 Promotion): Promoted ahead of InferenceNet and Parasail because Cloudflare delivers a 97% cache discount ($0.003/M cacheRead), 3× lower completion costs ($0.60/M vs. $1.20/M on Parasail), and a massive 943,718 completion token limit.
      • Fireworks (#4 Addition): Onboarded as a high-reliability enterprise tier-1 provider ($0.30/M prompt, $1.20/M completion, $0.006/M cacheRead, 100% uptime, 943k max completion tokens, full tool choice and structured outputs support).
      • Completion Token Headroom Guardrail (maxTokens: 65536): Because reasoning models share the completion token budget between internal chain-of-thought (reasoningTokens, typically 2k–4k tokens) and the actual tool-call payload, omitting maxTokens previously allowed OpenClaw/OpenRouter to default to an 8,192 token ceiling. During large multi-kilobyte code or website generation, hitting 8,192 tokens abruptly truncated tool-call JSON mid-payload (Provider returned an incomplete or malformed tool call). Explicitly configuring maxTokens: 65536 and params.max_tokens: 65536 provides ample headroom for complex reasoning and large outputs.
      • Live OpenRouter routing forensics verified that morph/fp8 is filtered out by OpenRouter's Filter by Tool Compatibility when tools are passed, deepseek (official) is filtered by account privacy guardrails (Paid model training violation), and baseten returns HTTP 429. Meanwhile, deepinfra, cloudflare, inference-net, fireworks, and parasail deliver 100% tool-calling reliability and sub-second to 3s latency.
    • Post-Trial Evaluation of z-ai/glm-5.3-flash (2026-09-13 to 2026-10-17):
      • While z-ai/glm-5.3-flash was trialed as primary conductor, 17-day production telemetry (Oct 1–17, 2026) revealed 12 mid-stream tool-call aborts (stopReason: "error", errorMessage: "Provider returned an incomplete or malformed tool call") across 5 agents (main, grabengasse_media, koch, hebamme, doktor) when generating nested tool_call({ id, args }) payloads after tool_describe, as well as occasional thinking-only whitespace stalls (Issue 23) and long-context attention drift (Issues 22 & 25).
      • Promoting deepseek/deepseek-v4.1-flash-1017 to primary across all agents eliminates malformed tool-call stream aborts AND lowers total monthly cost by >50% compared to GLM 5.3 Flash.
    • Decoupled Vision Override: deepseek/deepseek-v4.1-flash-1017, deepseek/deepseek-v4.1-flash, and z-ai/glm-5.3-flash are registered with input: ["text"] under models.providers.openrouter.models so that OpenClaw always routes user photos through tools.media.image (google/gemini-3.8-flash) for high-precision German OCR before handing enriched text to the chat model (Issue 29).
  • Fallbacks:
    1. openrouter/deepseek/deepseek-v4.1-flash (0910 baseline across 25+ providers, pinned to deepinfra, streamlake, inference-net, digitalocean, venice, nebius).
    2. openrouter/z-ai/glm-5.3-flash: Secondary fallback pinned to fp8/bf16 providers (morph, near-ai, digitalocean, venice, streamlake).
    3. openrouter/qwen/qwen3.8-flash: Provider diversity.

2. Autonomous Subagents (Tier 2)

  • All Subagents & Specialists: openrouter/deepseek/deepseek-v4.1-flash-1017
    • Execution: Spawned via sessions_spawn with a 300-second timeout. Provides high-precision reasoning, rock-solid nested JSON tool loops (tool_search -> tool_describe -> tool_call), and zero syntax drift.
  • Fallback: openrouter/deepseek/deepseek-v4.1-flash / openrouter/z-ai/glm-5.3-flash / openrouter/qwen/qwen3.8-flash.

3. Background Daemon & Heartbeats (Tier 3)

  • Configuration: heartbeat.model: "openrouter/deepseek/deepseek-v4.1-flash-1017", heartbeat.target: none, heartbeat.lightContext: true.
  • Rationale: Because background heartbeats have a 75%+ cacheRead share (22.86M cacheRead vs. 7.73M uncached input over 17 days), DeepSeek V4.1 Flash 1017's $0.00294/M cache read rate on DeepInfra reduces 17-day heartbeat cost to <$0.90 while keeping prompt cache warm on the primary provider.

4. Vision Input vs. Text-to-Image Generation (Tier 4)

  • Crucial Distinction:
    • Vision Input (imageModel vs. Dedicated Primary): Reading photos sent to Telegram (e.g. food ingredients, nutrition tables, plants, receipts). Note: OpenClaw only invokes imageModel if the primary model lacks image input modality. Since z-ai/glm-5.3-flash advertises image modality on OpenRouter, OpenClaw does not auto-divert to imageModel. Therefore, vision-intensive agents (specifically koch) must explicitly configure primary: "openrouter/google/gemini-3.8-flash" to leverage Google's SOTA OCR and dense-table recognition.
    • Text-to-Image Synthesis (mediaModels.image): Creating new pictures upon user request (e.g. "Zeichne mir ein Profilbild von Octobot"). Chat LLMs return 404 here.
  • Image Synthesis Engine:
    • Primary: openrouter/google/gemini-3.1-flash-image (Nano Banana 2).
      • Cost: $0.00006 per generated image + $0.50/1M prompt.
      • Latency: ~4.5 seconds.
    • Fallbacks:
      1. openrouter/google/gemini-3.1-flash-image-preview
      2. openrouter/google/gemini-3-pro-image (Nano Banana Pro)
    • Decommissioned: Gemini 2.5 is completely removed from all image configurations.

5. Voice, Audio & Podcast Synthesis (Tier 5)

  • Speech-to-Text (Audio Input):
    • Engine: openrouter/openai/whisper-large-v3-turbo
    • Capabilities: Fast, highly accurate transcription of German and English Telegram voice notes, with local gateway fallback.
  • Text-to-Speech (Audio Dossiers & Podcasts):
    • Engine: google/gemini-3.1-flash-tts-preview via text-to-audio skill.
    • Voices:
      • German Solo/Host: Puck (warm, natural, dynamic).
      • German Deep/Calm: Charon.
      • German Co-Host / Expert: Aoede.
      • English: Zephyr.
    • Fallback: minimax/speech-2.8-hd for studio-grade multi-speaker podcasts when Google TTS is throttled.

6. Grounded Web Search & Vector Embeddings

  • Live Search: perplexity/sonar-pro via OpenRouter (replacing raw web scraping with grounded citations).
  • Memory Embeddings: text-embedding-3-small via OpenRouter for the Obsidian personal knowledge wiki.

Context Lifecycle & Daytime Headroom Policy

To balance the user's desire to retain full conversation context across the entire day while avoiding the 70-second latency penalty that uncompacted transcripts impose on OpenRouter providers:

  1. Daytime Headroom (maxActiveTranscriptBytes: "3mb"): The active transcript is permitted to retain the day's back-and-forth interactions so OpenClaw recalls earlier topics and instructions naturally.
  2. Deterministic Nightly Compaction: Scheduled daily at 03:30 AM Europe/Berlin (ce39bbf8-b83f-453e-be53-04e27db27b78):
    openclaw sessions compact "agent:main:main" --max-lines 250
    
    Executes in 2.1 seconds using 0 LLM tokens, ensuring every morning begins with a lean, fast ~15k token transcript.

Open Clarifying Questions for Moritz

To align on remaining edge cases before final sign-off:

  1. Active Trial with GLM-5.3-Flash as Primary Conductor:
    • As requested, z-ai/glm-5.3-flash is now deployed as the Primary Conductor for interactive chat and autonomous subagents, equipped with the prompt guardrails against direct tool calls and XML leakage.
    • deepseek/deepseek-v4.1-flash stands ready as the zero-latency primary fallback.
    • We will observe real-world performance, tool adherence, and cost over the next several days.
  2. Thinking / Reasoning Mode on Telegram:
    • Currently, thinking: medium is enabled, emitting 100–300 reasoning tokens before generating conversational text.
    • Question: Would you like to disable thinking mode (thinking: none) for standard Telegram chat turns (for near-instant replies) and keep reasoning active only for subagents?
  3. On-Demand Frontier Model:
    • With all standard workloads handled by Flash-tier models, do you ever want an explicit escape hatch to a frontier model (e.g. claude-3.7-sonnet or gemini-3.1-pro-preview triggered exclusively via /expert <query>)?

OpenRouter Provider Qualification Criteria & Re-Evaluation Playbook

Whenever a new model snapshot (such as deepseek/deepseek-v4.1-flash-1017) is evaluated or re-checked as new hosting providers join OpenRouter, every candidate provider MUST pass the following 7-point qualification gate before being added to extra_body.provider.order in openclaw-instance.secret.yaml:

1. Hard Technical Gates (Must Pass All)

  1. Quantization Precision (fp8 / bf16 Only):
    • Rule: Must run fp8 or bf16 (or verified full-precision unknown from trusted cloud hosts like Cloudflare).
    • Reject: Any fp4, nvfp4, mxfp4, or int4 quantization (decart, sail-research, wafer, open-inference, phala fp4). 4-bit quantizations severely degrade nested JSON tool-call syntax and German grammar.
  2. OpenRouter Routing Funnel & Guardrail Compatibility (tools + tool_choice: "auto" Pass):
    • Rule: Must not be stripped by OpenRouter's internal routing filters (Filter by Tool Compatibility or Filter by Guardrails) when a request includes tools: [...] and tool_choice: "auto".
    • Why: Metadata in /api/v1/models/.../endpoints can claim supports_tool_choice: true even when OpenRouter's live router blocks the provider on actual tool calls (e.g. morph/fp8 is stripped by Filter by Tool Compatibility, and deepseek official is stripped by Paid model training violation account privacy settings).
  3. Nested MCP tool_call Serialization Integrity (2/2 Live Pass):
    • Rule: Must cleanly emit finish_reason: "tool_calls" with valid, parseable nested JSON arguments ({"id": "mcp:...", "args": {...}}) across back-to-back test runs without mid-stream SSE aborts (Provider returned an incomplete or malformed tool call) or HTTP 429 upstream rate limits.
  4. Context Window & Completion Headroom:
    • Rule: context_length >= 262,144 (1,048,576 preferred) and max_completion_tokens >= 131,072.
    • Why: Active daytime sessions with system prompts and tool schemas reach 20k–80k tokens.
  5. Uptime & Reliability:
    • Rule: uptime_last_1d >= 95% and uptime_last_30m == 100%.

2. Economic & Latency Ranking (How to Order Passing Providers)

Among providers passing all 5 technical gates, rank them in extra_body.provider.order by effective production cost and turn latency:

  1. Implicit Prompt Cache Read Rate (input_cache_read) — Highest Weight:
    • Because 73%–94% of OpenClaw's token volume (both interactive multi-turn chats and 30m background heartbeats) consists of cached prompt reads (cacheRead), a provider with supports_implicit_caching: true and a 95%–98% cache discount (<= $0.005 / 1M tokens, e.g. deepinfra at $0.00294/M or streamlake at $0.0030/M) is vastly cheaper in net monthly spend than a provider with slightly cheaper uncached input but weak or missing cacheRead discounts (e.g. nebius with cache_read: null or modal/primeintellect at $0.030/M).
  2. Base Prompt & Completion Pricing:
    • Target prompt <= $0.18 / 1M and completion <= $0.65 / 1M for top-priority slots; allow higher-priced providers (<= $0.30 / $1.20) only in lower fallback slots (#3/#4) for outage resilience.
  3. End-to-End Tool-Call Latency:
    • Target 1.0s – 5.5s for a ~300-token reasoning + tool-call turn.

3. One-Shot Live Verification Command (For Future Checks)

Any agent (including a fast/lightweight model) can audit all currently available OpenRouter endpoints for deepseek/deepseek-v4.1-flash-1017 in one step by running:

# 1. Inspect current endpoint metadata, pricing, cache_read, and 24h uptime:
curl -s https://openrouter.ai/api/v1/models/deepseek/deepseek-v4.1-flash-1017/endpoints | \
  jq -r '.data.endpoints[] | "\(.provider_name) | tag=\(.tag) | quant=\(.quantization) | ctx=\(.context_length) | prompt=\(.pricing.prompt) | comp=\(.pricing.completion) | cache=\(.pricing.input_cache_read) | uptime1d=\(.uptime_last_1d)"'

# 2. Live-test candidate providers inside openclaw-0 with real nested tool_call payloads:
kubectl exec openclaw-0 -n openclaw -c openclaw -- python3 -c '
import os, json, time, urllib.request
api_key = os.environ["OPENROUTER_API_KEY"]
tools = [{"type": "function", "function": {"name": "tool_call", "parameters": {"type": "object", "properties": {"id": {"type": "string"}, "args": {"type": "object"}}, "required": ["id", "args"]}}}]
candidates = ["deepinfra", "streamlake", "inference-net", "venice", "digitalocean", "novita", "parasail", "cloudflare", "nebius"]
for prov in candidates:
    req = urllib.request.Request(
        "https://openrouter.ai/api/v1/chat/completions",
        data=json.dumps({
            "model": "deepseek/deepseek-v4.1-flash-1017",
            "messages": [{"role": "user", "content": "Call tool_call with id=\"mcp:homeassistant:homeassistant__ha_get_state\" and args={\"entity_id\": \"media_player.bad\"}"}],
            "tools": tools, "tool_choice": "auto", "max_tokens": 250,
            "provider": {"order": [prov], "allow_fallbacks": False}
        }).encode(),
        headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
    )
    t0 = time.time()
    try:
        with urllib.request.urlopen(req, timeout=20) as r:
            b = json.loads(r.read().decode())
            tc = b["choices"][0]["message"].get("tool_calls")
            args = tc[0]["function"]["arguments"] if tc else "NONE"
            print(f"PASS | {prov:15s} | {time.time()-t0:4.2f}s | actual={b.get(\"provider\")} | args={args}")
    except urllib.error.HTTPError as e:
        print(f"FAIL | {prov:15s} | HTTP {e.code} | {e.read().decode()[:140]}")
    except Exception as e:
        print(f"FAIL | {prov:15s} | ERR {e}")
'

Consequences

Positive

  • Unified Latest-Generation Stack: Completely eliminates legacy Gemini 2.5 models; standardizes exclusively on DeepSeek V4.1 Flash 1017, Gemini 3.8/3.1, GLM 5.3, Whisper V3 Turbo, and Sonar Pro.
  • 100% Reliable Image Generation: Correctly routes image generation requests to Nano Banana 2 (google/gemini-3.1-flash-image), completely eliminating HTTP 404 generation errors.
  • Cohesive Voice & Audio Pipeline: Clear architectural documentation of the Whisper V3 Turbo and Gemini 3.1 Flash TTS integration for dossiers and podcasts.
  • Guaranteed Cost Ceiling: Drops weekly expenditure from $12.55 to ~$0.80–$1.40 (~90% savings) through DeepSeek V4.1 Flash 1017's $0.00294/M cache reads, decoupled heartbeats, and nightly compaction.

Negative & Mitigations

  • OpenRouter Provider Rollout Lag on Day-0 Model Releases: Newly released snapshots initially launch with 5–10 providers before the full fleet (25+ providers) joins over subsequent days.
    • Mitigation: Codified the 7-point provider qualification gate and one-shot test script above, and configured automatic model fallback to the 0910 snapshot (openrouter/deepseek/deepseek-v4.1-flash).