# Cache anthropic api for repeated prompt previews

> Short TTL cache on identical preview completions (non-production). For "cache anthropic api for repeated prompt previews", store ANTHROPIC_API_KEY in Secret Vault, call https://api.anthropic.com/v1/messages from a Worker, and let Framer hit only https://realcomposer.com/v1/workspace/cache-anthropic-api-prompt-previews 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/cache-anthropic-api-prompt-previews
- Client platform: Framer
- Upstream services: Anthropic
- Category: Edge Caching & Rate Limits
- JSON version: https://realcomposer.com/recipes/cache-anthropic-api-prompt-previews.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=cache-anthropic-api-prompt-previews&via=agent

## Problem: Anthropic rate limits from Framer traffic

Without edge caching, identical Anthropic reads during a session still round-trip to the origin.

- HTTP 429 responses during launch traffic
- Surprise SaaS overages from duplicate reads
- Slower repeat views than necessary

## Solution steps

1. **Store upstream credentials in Secret Vault**: Create ANTHROPIC_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Configure upstream steps and output mapping**: Use POST against https://api.anthropic.com/v1/messages. Allowed origins on the client token should match post pipelines set cache ttl to 0 because realcomposer only stores get responses at the edge.

3. **Fetch inside Framer overrides**: Authorize with Bearer rc_live_ and fetch the mapped JSON contract your UI already expects.

```
fetch('https://realcomposer.com/v1/workspace/cache-anthropic-api-prompt-previews', {
  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": "Cache anthropic api for repeated prompt previews",
  "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": 10000
    }
  ],
  "outputMapping": {
    "version": 1,
    "fields": [
      {
        "targetPath": "role",
        "sourcePath": "content.0.text",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "model",
        "sourcePath": "model",
        "type": "string",
        "sourceStep": "anthropic"
      },
      {
        "targetPath": "stopReason",
        "sourcePath": "stop_reason",
        "type": "string",
        "sourceStep": "anthropic"
      }
    ]
  }
}
```

## Call the pipeline

```bash
curl -X POST "https://realcomposer.com/v1/workspace/cache-anthropic-api-prompt-previews" \
  -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_cache-an",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "cache-anthropic-api-prompt-previews"
  },
  "payload": {
    "id": "sample_id",
    "status": "active",
    "nested": {
      "tags": [
        "demo"
      ],
      "internal": true
    },
    "unused_blob": {
      "foo": "bar",
      "bar": "baz"
    }
  }
}
```

Slimmed RealComposer response:

```json
{
  "role": "sample",
  "model": "sample",
  "stopReason": "sample"
}
```

## FAQ

### Is it safe to log Anthropic responses in Framer?

Store ANTHROPIC_API_KEY for cache-anthropic-api-prompt-previews. Framer never sees decrypted values.

### When does edge caching apply to this recipe?

cache-anthropic-api-prompt-previews vault headers attach server-side, but Anthropic or OpenAI model and messages must come from the client body.

### Can I restrict which domains call this pipeline?

Upstream steps in cache-anthropic-api-prompt-previews honor timeoutMs and optional fallback JSON without leaking secrets.
