# Anthropic messages api response slim for chat ui

> Map content blocks and stop reason; drop billing debug fields. For "anthropic messages api response slim for chat ui", store ANTHROPIC_API_KEY in Secret Vault, call https://api.anthropic.com/v1/messages from a Worker, and let Next.js / React hit only https://realcomposer.com/v1/workspace/slim-anthropic-messages-response-chat 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/slim-anthropic-messages-response-chat
- Client platform: Next.js / React
- Upstream services: Anthropic
- Category: Payload Slimmer
- JSON version: https://realcomposer.com/recipes/slim-anthropic-messages-response-chat.json
- Install in RealComposer (free plan, pre-fills this pipeline): https://realcomposer.com/dashboard/pipelines/new?recipe=slim-anthropic-messages-response-chat&via=agent

## Problem: Unmapped Anthropic fields waste bandwidth

Anthropic returns nested CRM, audit, and configuration objects. Next.js / React parses them on the main thread even when the UI binds three fields.

- Large arrays kept in memory after navigation
- Analytics showing poor LCP on API-heavy screens
- Developers shipping defensive null checks for unused paths

## Solution steps

1. **Bind vault keys to the Worker**: Create ANTHROPIC_API_KEY vault entries, then bind them to this pipeline step headers with {{secrets.NAME}} placeholders.

2. **Target the real upstream URL**: 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. **Call from Next.js / React**: Call https://realcomposer.com/v1/workspace/slim-anthropic-messages-response-chat with a restricted rc_live_ client token. Next.js / React never sends upstream secrets.

```
// app/api route or client component
const res = await fetch('https://realcomposer.com/v1/workspace/slim-anthropic-messages-response-chat', {
  cache: 'no-store',
  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": "Anthropic messages api response slim for chat ui",
  "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/slim-anthropic-messages-response-chat" \
  -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_slim-ant",
  "upstreamLatencyMs": 142,
  "audit": {
    "region": "edge",
    "trace": "slim-anthropic-messages-response-chat"
  },
  "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 Next.js / React?

Point Next.js / React at /recipes/slim-anthropic-messages-response-chat edge URL. RealComposer answers OPTIONS with Access-Control headers for allowed origins.

### Will Anthropic or Linear prompt bodies be rewritten?

slim-anthropic-messages-response-chat keeps cache TTL at 0 because it uses POST upstream calls or per-user responses. Add ids to query strings before enabling GET cache.

### How do I rotate upstream credentials?

Rotate vault secrets for slim-anthropic-messages-response-chat anytime; clients keep the same rc_live_ token.
