Latest articles

How to make requests to Gemini using the Claude (Anthropic) SDK

20 March 2026Braintrust Team

TL;DR: The Braintrust AI Gateway lets you call Google's Gemini models using Anthropic's SDK. Point your Anthropic client at https://gateway.braintrust.dev without /v1, since the Anthropic SDK appends its own path prefix, authenticate with your Braintrust API key, and pass a Gemini model name in the model parameter. Braintrust converts the Anthropic request format into Gemini's API format and returns the response in Anthropic's standard structure, so your existing client logic continues to work with Gemini through the same Anthropic SDK.

Keep your Anthropic SDK setup and access Gemini through Braintrust

Many teams already use the Anthropic SDK in production because their application is built around Claude's message-based workflow. Prompt construction, structured output handling, and response parsing all follow Anthropic's patterns, and production code often relies on SDK-specific behaviors such as content block types and streaming event shapes. Pulling Gemini's SDK into that stack means maintaining two parallel request pipelines and two sets of response-handling logic for what should be a simple model swap.

Braintrust AI Gateway lets engineers use Gemini through the same messages.create() workflow already used for Claude. Instead of adding Google's SDK and supporting a second request and response pattern in the application, developers can keep the Anthropic SDK in place and call Gemini through the same interface. Braintrust handles the format conversion at the gateway, stores provider credentials in Braintrust settings, and keeps the application on one SDK.

How to make requests to Gemini using the Anthropic SDK

Braintrust AI Gateway lets you send Gemini requests through the Anthropic SDK, so you can keep Anthropic's request pattern while accessing Gemini models. The steps below cover the setup needed to send your first Gemini request through the Anthropic SDK.

Prerequisites

1. Create a Braintrust account and generate an API key

Sign up at Braintrust, then go to Settings > Organization > API keys and click + API key. Enter a name for the key, click Create, and copy it immediately because Braintrust will not show it again. The key, prefixed with sk-, is used by your application to authenticate requests sent through the Braintrust AI Gateway.

For production use cases, such as CI/CD pipelines or backend services, create a service token under Settings > Organization > Service tokens. Assign the token to the appropriate permission groups, then click Create. Service tokens use the bt-st- prefix and work anywhere API keys are accepted.

2. Add your Google Gemini API key to Braintrust

Go to Settings > Organization > AI providers and open the Google/Gemini provider. Enter your Gemini API key and click Save. Braintrust uses this stored key when sending requests to Google, so your Gemini credential does not need to be embedded in application code. Braintrust encrypts provider API keys using AES-256 with unique keys and nonces.

3. Install the Anthropic SDK

Run npm install @anthropic-ai/sdk for TypeScript or JavaScript, or pip install anthropic for Python.

Calling Gemini with the Anthropic SDK through Braintrust

When calling Gemini with the Anthropic SDK, the main Anthropic-specific difference is the base URL: https://gateway.braintrust.dev without /v1 because the Anthropic SDK appends its own path automatically.

With the base URL configured, the request still follows Anthropic's usual messages.create() pattern, including fields such as messages and max_tokens. When the model field contains a Gemini model name, Braintrust recognizes that the request should be routed to Google and returns the result in Anthropic's response format, which means existing parsing logic such as response.content[0].text continues to work without any changes.

typescript

const client = new Anthropic({
  baseURL: "https://gateway.braintrust.dev",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

async function main() {
  // Call Google's Gemini using the Anthropic SDK
  const response = await client.messages.create({
    model: "gemini-2.5-pro",
    messages: [{ role: "user", content: "Hello!" }],
    max_tokens: 50,
  });

  console.log(response.content[0].type === "text" ? response.content[0].text : "");
}

main();

Here is the equivalent in Python.

python
import os

from anthropic import Anthropic

client = Anthropic(
    base_url="https://gateway.braintrust.dev",
    api_key=os.environ["BRAINTRUST_API_KEY"],
)

# Call Google's Gemini using the Anthropic SDK
response = client.messages.create(
    model="gemini-2.5-pro",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=50,
)

print(response.content[0].text)

Enabling logging and caching

Logging sends each request trace to a Braintrust project, where you can review Gemini calls alongside the rest of your model traffic. To attach the request to a Braintrust trace, add the x-bt-parent header using Braintrust's logger.

typescript


const logger = initLogger({ projectName: "My Project" });

async function main() {
  await logger.traced(async (span) => {
    const client = new Anthropic({
      baseURL: "https://gateway.braintrust.dev",
      apiKey: process.env.BRAINTRUST_API_KEY,
    });

    // Call Google's Gemini using the Anthropic SDK
    const response = await client.messages.create(
      {
        model: "gemini-2.5-pro",
        messages: [{ role: "user", content: "Hello!" }],
        max_tokens: 50,
      },
      {
        headers: {
          "x-bt-parent": await span.export(),
        },
      },
    );

    console.log(
      response.content[0].type === "text" ? response.content[0].text : "",
    );
  });
}

main();

Each trace records token usage, latency, cost, and the full request and response, all of which appear in your Braintrust project's logs page.

Caching is useful during prompt iteration because it prevents sending the same request to the provider multiple times. To cache responses, set "x-bt-use-cache": "always" in your client's defaultHeaders. Braintrust stores cached responses using AES-GCM encryption with a key derived from your API key and ties each cache entry to the API key that created it. Cache entries remain available for one week by default, and the x-bt-cache-ttl header lets you set a different duration in seconds for a specific request.

Use Braintrust AI Gateway to access more LLM models through one SDK

Calling Gemini through the Anthropic SDK is one example of how Braintrust AI Gateway lets teams keep one SDK while accessing models from another provider. The same approach works across any supported SDK and AI provider combination.

Braintrust's AI Gateway accepts requests from the OpenAI SDK, the Anthropic SDK, and the Google Gemini SDK. You can keep the SDK already built into your application, point that client to Braintrust's gateway URL, and switch providers by changing the model name in the existing code.

Braintrust supports direct integrations with major model providers, including OpenAI, Anthropic, Google, Mistral, Groq, Fireworks, Together, xAI, Perplexity, Replicate, Cerebras, Baseten, and Lepton. It also supports cloud platform providers, including AWS Bedrock, Google Vertex AI, Azure OpenAI, and Databricks. If more LLM models are required, Braintrust's custom provider configuration supports self-hosted models, fine-tuned models, and proprietary AI endpoints.

Provider credentials are stored in Braintrust's organization settings and never appear in application code, so onboarding a new provider requires adding the key in the dashboard and referencing the new model name in your requests.

Use new model providers without building a new SDK integration each time. Start free with Braintrust today.

FAQs

Do I need separate API keys for Anthropic and Gemini when using the Braintrust AI Gateway?

Your application only needs a Braintrust API key. Anthropic and Gemini credentials are added separately in Braintrust under AI Providers, keeping provider keys out of application code and giving you one place to manage provider authentication.

Can I use both Claude and Gemini in the same application through Braintrust?

Both models are accessible through the same Anthropic client instance. To switch between Claude and Gemini, change the model parameter in your messages.create() call. Braintrust routes each request to the correct provider based on the model name, so a single client configuration handles both.

How do I track per-provider costs when using multiple models?

Enable logging on your Braintrust project, and each request records its token usage and cost. The Logs dashboard groups this data by model and provider, making it straightforward to see how Claude and Gemini spending breaks down across your workloads.