# Combine anthropic claude with openai fallback router

> Primary Anthropic Messages step plus OpenAI fallback step in one pipeline. For "combine anthropic claude with openai fallback router", store ANTHROPIC_API_KEY, OPENAI_API_KEY in Secret Vault, call https://api.anthropic.com/v1/messages from a Worker, and let Frontend / Web hit only https://realcomposer.com/v1/workspace/combine-anthropic-with-openai-fallback with an rc_live_ client token. POST upstream calls are not stored in caches.default; clients must send JSON bodies and headers forward unchanged.

- Canonical page: https://realcomposer.com/recipes/combine-anthropic-with-openai-fallback
- Client platform: Frontend / Web
- Upstream services: Anthropic, OpenAI
- Category: Multi-API Aggregation
- JSON version: https://realcomposer.com/recipes/combine-anthropic-with-openai-fallback.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=combine-anthropic-with-openai-fallback&via=agent

## Problem: Serial Anthropic and OpenAI calls slow Frontend / Web pages

Each extra Anthropic and OpenAI hop adds hundreds of milliseconds and another OAuth refresh path your UI must manage.

- One slow upstream blocking an entire page
- Partial failures leaving UI in inconsistent states
- Duplicate auth refresh logic per service

## Solution steps

1. **Add vault secrets for this pipeline**: Store ANTHROPIC_API_KEY, OPENAI_API_KEY in the vault. Frontend / Web never embeds the raw secret, only a revocable rc_live_ client token.

2. **Target the real upstream URL**: Configure headersTemplate to inject vault secrets, target https://api.anthropic.com/v1/messages, and Add output mapping fields that mirror the JSON contract your screen already expects.

3. **Call from Frontend / Web**: Allowed origins on the token should list every Frontend / Web host that calls this URL.

```
const res = await fetch('https://realcomposer.com/v1/workspace/combine-anthropic-with-openai-fallback', {
  headers: { Authorization: 'Bearer rc_live_your_token' }
});
```

## Pipeline config

Import this config into a RealComposer pipeline. `{{secrets.NAME}}` values are read from the encrypted Secret Vault at request time, so the client never sees them.

```json
{
  "name": "Combine anthropic claude with openai fallback router",
  "method": "POST",
  "cacheTtlSeconds": 0,
  "authMode": "client_token",
  "steps": [
    {
      "stepKey": "anthropic",
      "upstreamUrlTemplate": "https://api.anthropic.com/v1/messages",
      "httpMethod": "POST",
      "headersTemplate": {
        "x-api-key": "{{secrets.ANTHROPIC_API_KEY}}",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json"
      },
      "timeoutMs": 15000
    },
    {
      "stepKey": "openai",
      "upstreamUrlTemplate": "https://api.openai.com/v1/chat/completions",
      "httpMethod": "POST",
      "headersTemplate": {
        "Authorization": "Bearer {{secrets.OPENAI_API_KEY}}",
        "Content-Type": "application/json"
      },
      "timeoutMs": 15000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "anthropic.role",
        "sourcePath": "content.0.text",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "anthropic.model",
        "sourcePath": "model",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "openai.reply",
        "sourcePath": "choices.0.message.content",
        "type": "string",
        "sourceStep": "openai"
      },
      {
        "targetPath": "openai.tokens",
        "sourcePath": "usage.total_tokens",
        "type": "number",
        "sourceStep": "openai"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/combine-anthropic-with-openai-fallback" \
  -H "Authorization: Bearer rc_live_client_token" \
  -H "Content-Type: application/json" \
  -d '{"model":"example"}'
```

## Payload before and after

Raw upstream response:

```json
{
  "requestId": "req_combine-",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "combine-anthropic-with-openai-fallback"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "anthropic_role": "sample",
  "anthropic_model": "sample",
  "openai_reply": "sample",
  "openai_tokens": "sample"
}
```

## FAQ

### Can Frontend / Web call Anthropic without holding the production key?

Use scoped tokens where the vendor allows them on combine-anthropic-with-openai-fallback. Admin keys stay {{secrets.*}} references only.

### How do I forward JSON bodies to POST upstreams?

combine-anthropic-with-openai-fallback POST steps bypass caches.default even if cacheTtlSeconds is set in the dashboard.

### How do I rotate upstream credentials?

Rotate vault secrets for combine-anthropic-with-openai-fallback anytime; clients keep the same rc_live_ token.
