API Reference
@chai-ai/openai
OpenAI model provider adapter for Chai AI SDK.
The @chai-ai/openai package provides the adapter layer for connecting Chai AI agents to OpenAI's models using the official openai Node SDK. Under the hood, this uses the modern Responses API structure.
Installation
Ensure you have installed the OpenAI provider package alongside chai-ai:
npm install @chai-ai/openaiAuthentication
The provider automatically reads the OpenAI API Key from the environment:
export OPENAI_API_KEY="your-openai-api-key"API Reference
openai(modelId)
Creates a LanguageModel descriptor for an OpenAI model.
- Parameters:
modelId: string— The identifier of the OpenAI model (e.g.,"gpt-4o-mini","gpt-4o").
- Returns:
LanguageModelinterface instance.
import { Agent } from "chai-ai";
import { openai } from "@chai-ai/openai";
const model = openai("gpt-4o-mini");
const agent = new Agent({
name: "openai-agent",
instructions: "You are an OpenAI-powered assistant.",
model,
});Features & Implementation Details
- Responses API Integration: Maps message history structures directly onto the OpenAI Responses API (
client.responses.create). - Client-Side Cancellation: Fully threads abort signals (
AbortSignal) into the request configuration context to terminate in-flight connections on timeout. - Token Usage Mapping: Automatically translates
usage.input_tokens,usage.output_tokens, andusage.total_tokensfrom OpenAI API responses into standard Chai AI SDKTokenUsagemetrics.