Skip to content

Update example for AI Gateway + BYOK + @ai-sdk/anthropic #511

@kamal

Description

@kamal

With the following set up:

  1. Authenticated Cloudflare AI Gateway
  2. Anthropic API token stored in AI Gateway's Provider Key
  3. @ai-sdk/anthropic

To successfully set up @ai-sdk/anthropic, you need to specify

const anthropic = createAnthropic({
  apiKey: "",
  baseURL: await env.AI.gateway(env.GATEWAY_ID).getUrl("anthropic"),
  headers: {
    "cf-aig-authorization": `Bearer ${env.GATEWAY_API_KEY}`,
  }
});
  • apiKey is needed even if it's an empty string even though we've provided the Anthropic api key to AI Gateway. ai-sdk will throw an exception if it's not defined (and ANTHROPIC_API_KEY doesn't exist in env)
  • passing in custom header for cf-aig-authorization is needed if running authenticated AI Gateway

It would be ideal if AI Gateway will take the gateway API key as apiKey and move it to x-api-token so that the definition could just be

const anthropic = createAnthropic({
  apiKey: GATEWAY_API_KEY,
  baseURL: await env.AI.gateway(env.GATEWAY_ID).getUrl("anthropic")
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions