OpenAI
Install
Section titled “Install”npm install agentfootprint openaiServer-side (Node.js)
Section titled “Server-side (Node.js)”import { Agent, openai, defineTool } from 'agentfootprint';
const provider = openai('gpt-4o', { apiKey: process.env.OPENAI_API_KEY,});
const agent = Agent.create({ provider }) .system('You are a helpful assistant.') .tool(myTool) .streaming(true) .build();
const result = await agent.run('Hello');Browser
Section titled “Browser”No SDK dependency — calls the OpenAI REST API directly:
import { Agent, BrowserOpenAIAdapter } from 'agentfootprint';
const provider = new BrowserOpenAIAdapter({ apiKey: userApiKey, model: 'gpt-4o',});
const agent = Agent.create({ provider }).build();Available models
Section titled “Available models”| Model | ID | Best for |
|---|---|---|
| GPT-4o | gpt-4o | General purpose |
| GPT-4o Mini | gpt-4o-mini | Fast, low cost |
| GPT-4.1 | gpt-4.1 | Coding, instruction following |
| GPT-4.1 Mini | gpt-4.1-mini | Budget coding |
| GPT-4.1 Nano | gpt-4.1-nano | Fastest, cheapest |
| o3 | o3 | Complex reasoning |
| o3 Mini | o3-mini | Fast reasoning |
| o4 Mini | o4-mini | Latest reasoning |
Switching from the OpenAI SDK
Section titled “Switching from the OpenAI SDK”import OpenAI from 'openai';
const client = new OpenAI();const response = await client.chat.completions.create({ model: 'gpt-4o', messages: [{ role: 'user', content: 'Hello' }],});console.log(response.choices[0].message.content);import { Agent, openai } from 'agentfootprint';
const agent = Agent.create({ provider: openai('gpt-4o'),}) .system('You are helpful.') .build();
const result = await agent.run('Hello');console.log(result.content);console.log(agent.getNarrative()); // full traceObservability
Section titled “Observability”Track tokens, cost, and tool usage:
import { Agent, openai } from 'agentfootprint';import { agentObservability } from 'agentfootprint/observe';
const obs = agentObservability();
const agent = Agent.create({ provider: openai('gpt-4o'),}) .system('You are helpful.') .recorder(obs) .build();
await agent.run('Hello');
console.log(obs.tokens()); // { totalCalls: 1, totalInputTokens: 18, ... }console.log(obs.cost()); // USD estimateProvider failover
Section titled “Provider failover”Cross-provider resilience — the order is configurable:
import { anthropic, openai } from 'agentfootprint';import { fallbackProvider } from 'agentfootprint/resilience';
// OpenAI primary, Claude fallbackconst provider = fallbackProvider([ openai('gpt-4o'), anthropic('claude-sonnet-4-20250514'),]);
// Or Claude primary, OpenAI fallback — swap the orderconst provider = fallbackProvider([ anthropic('claude-sonnet-4-20250514'), openai('gpt-4o'),]);What you gain: Same agent code works across providers. Swap one line to switch. Add fallback for resilience.