Connect VoltAgent to the AI Gateway
Send requests from your VoltAgent agents through the Logfire AI Gateway.
VoltAgent is a TypeScript framework for building AI agents with tool use and its own observability features. The example creates an OpenAI-compatible provider that uses the gateway key and route.
- Complete the AI Gateway prerequisites, including setting
LOGFIRE_GATEWAY_API_KEYin your terminal. - Use an existing VoltAgent project with the packages imported below 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 { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { Agent } from '@voltagent/core';
import { z } from 'zod';
const envSchema = z.object({
LOGFIRE_GATEWAY_API_KEY: z.string(),
});
const env = envSchema.parse(process.env);
const gateway = createOpenAICompatible({
name: 'logfire-gateway',
apiKey: env.LOGFIRE_GATEWAY_API_KEY,
baseURL: 'https://gateway-us.pydantic.dev/proxy/openai',
});
const agent = new Agent({
name: 'weather-agent',
instructions: 'A helpful assistant that answers questions.',
model: gateway.chatModel('gpt-5.4-mini'),
});
const response = await agent.generateText('What is the weather in London?');
console.log(response.text);
Run the 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.