Skip to main content

Overview

OpenClaw is a popular open-source AI agent platform. Layer AI integrates as a model provider, giving you cost controls, automatic fallbacks, smart routing, and full observability over your OpenClaw agent’s AI usage.

Why Layer for OpenClaw?

OpenClaw users commonly face:
  • Cost anxiety — No spending controls, surprise bills from long-running agents
  • Reliability risk — Single provider means downtime when APIs fail
  • No visibility — No insight into per-request costs, token usage, or model performance
Layer solves all three:
  • Spending limits — Gate-level and account-level cost caps prevent runaway spend
  • Automatic fallbacks — If one provider fails, Layer routes to the next model in your fallback chain
  • Smart routing — The Architect recommends optimal models based on your usage patterns
  • Full observability — Every request logged with cost, latency, tokens, and model used

Quick Start

Step 1: Create a Layer Account and API Key

  1. Sign up at uselayer.ai
  2. Go to Dashboard → API Keys → Create New Key
  3. Copy your API key (format: layer_xxx)

Step 2: Create a Gate

Create a gate for your OpenClaw agent:
  1. Go to Dashboard → Gates → Create New Gate
  2. Configure:
    • Task typechat
    • Model — Choose your primary model (e.g., claude-sonnet-4-20250514, gpt-4o)
    • Fallback models — Add fallbacks for reliability (e.g., gpt-4ogemini-2.0-flash)
    • Spending limit — Set a daily or monthly cap
  3. Copy the gate ID (UUID)

Step 3: Configure OpenClaw

Edit your OpenClaw configuration file (~/.openclaw/openclaw.json):
{
  "env": {
    "LAYER_API_KEY": "layer_your_api_key"
  },
  "models": {
    "mode": "merge",
    "providers": {
      "layer": {
        "baseUrl": "https://api.uselayer.ai/v1",
        "apiKey": "LAYER_API_KEY",
        "api": "openai-completions",
        "authHeader": true,
        "models": [
          {
            "id": "your-gate-uuid",
            "name": "My Layer Gate",
            "api": "openai-completions",
            "contextWindow": 200000,
            "maxTokens": 8192
          }
        ]
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {
        "primary": "layer/your-gate-uuid"
      }
    }
  }
}
Replace layer_your_api_key with your Layer API key and your-gate-uuid with your gate ID.
Both api fields must be "openai-completions" — at the provider level and the model level. Mismatching these causes silent failures where the agent appears to complete instantly (~7ms) without actually making a request.

Step 4: Restart and Test

openclaw gateway restart
openclaw dashboard
Your OpenClaw agent now routes all requests through Layer. Check Dashboard → Logs to see requests flowing.

How It Works

Layer provides an OpenAI-compatible endpoint (/v1/chat/completions) that OpenClaw connects to natively. The request flow:
OpenClaw → /v1/chat/completions (Layer) → Your configured model provider → Response back to OpenClaw
Layer handles:
  • Model resolution — Your gate ID maps to the configured model, fallbacks, and parameters
  • Streaming — Full SSE streaming support across all providers
  • Tool calling — Function/tool calls pass through transparently
  • Cost tracking — Every request logged with actual cost based on token usage

Multiple Gates

You can configure multiple gates for different OpenClaw tasks:
{
  "models": {
    "providers": {
      "layer": {
        "baseUrl": "https://api.uselayer.ai/v1",
        "apiKey": "LAYER_API_KEY",
        "api": "openai-completions",
        "authHeader": true,
        "models": [
          {
            "id": "gate-uuid-for-coding",
            "name": "Coding (Sonnet)",
            "api": "openai-completions",
            "contextWindow": 200000,
            "maxTokens": 8192
          },
          {
            "id": "gate-uuid-for-research",
            "name": "Research (GPT-4o)",
            "api": "openai-completions",
            "contextWindow": 128000,
            "maxTokens": 4096
          }
        ]
      }
    }
  }
}
Each gate can have its own model, fallback chain, spending limit, and routing strategy.

Dashboard

Layer provides an OpenClaw integration page in the dashboard at Dashboard → Integrations → OpenClaw with:
  • Overview — Integration status, key metrics (gates, requests, cost, latency), and quick actions
  • Quick Start — Step-by-step setup guide with code examples

Troubleshooting

ProblemCauseFix
Agent completes in ~7ms with no outputAPI type mismatchEnsure both api fields are "openai-completions"
Model not changing after config updateCached sessionDelete sessions: rm ~/.openclaw/agents/main/sessions/*.jsonl
”Missing gateId” errorGate ID not in requestVerify the id field in your model config is a valid gate UUID
No requests in Layer dashboardGateway not routing to LayerCheck logs: tail -f ~/.openclaw/logs/gateway.log and look for provider=layer