Connect Strands Agents to the AI Gateway
Send requests from your Strands Agents application through the Logfire AI Gateway.
Strands Agents is AWS’s open-source framework for building AI agents. Both examples configure its OpenAI model provider with an OpenAI-compatible gateway route.
- Complete the AI Gateway prerequisites, including setting
LOGFIRE_GATEWAY_API_KEYin your terminal. - Use an existing Strands Agents project with the packages imported by your chosen example installed.
Set apiKey to your gateway key and baseURL to the OpenAI-compatible gateway route. Copy the route and a supported model name from the Gateway Connect tab.
import { Agent } from '@strands-agents/sdk';
import { OpenAIModel } from '@strands-agents/sdk/models/openai';
import { z } from 'zod';
const envSchema = z.object({
LOGFIRE_GATEWAY_API_KEY: z.string(),
});
const env = envSchema.parse(process.env);
const model = new OpenAIModel({
api: 'chat',
apiKey: env.LOGFIRE_GATEWAY_API_KEY,
clientConfig: {
baseURL: 'https://gateway-us.pydantic.dev/proxy/openai',
},
modelId: 'gpt-5.4-mini',
});
const agent = new Agent({ model, printer: false });
const response = await agent.invoke('What is the weather in London?');
console.log(response.lastMessage ?? response);
Set the equivalent api_key and base_url values on the Python model provider.
import os
from strands import Agent
from strands.models.openai import OpenAIModel
model = OpenAIModel(
client_args={
'api_key': os.environ['LOGFIRE_GATEWAY_API_KEY'],
'base_url': 'https://gateway-us.pydantic.dev/proxy/openai',
},
model_id='gpt-5.4-mini',
)
agent = Agent(model=model, callback_handler=None)
response = agent('What is the weather in London?')
print(response)
Run either example from your terminal. It prints the agent’s response. That confirms the client reached the gateway. Organization admins can also open AI Engineering > Gateway > Spending to see usage for the key. If telemetry is enabled, open the selected project’s Live view to inspect the request trace.
- The example cannot read
LOGFIRE_GATEWAY_API_KEY: set the environment variable in the same terminal where you run the example. - The request returns an authentication or model error: copy the URL and model name again from the Gateway Connect tab, and confirm that the selected route supports the OpenAI request format.