OpenAI Agents SDK (JS)
@openai/agents. It uses the Responses API.
Connect
Build a model object, so the SDK sends the id as given (its string lookup would read anthropic/... as a provider prefix). Turn tracing off with setTracingDisabled(true), or the SDK posts traces to OpenAI:
ts
/** The SDK model for one id. A model object sends the id as given: the SDK's own string lookup reads `anthropic/...` as a provider prefix. */
export function modelFor(client: OpenAI, model: string): OpenAIResponsesModel {
return new OpenAIResponsesModel(client, model)
}Chat
ts
const agent = new Agent({ name: "pong", model: modelFor(client, model), instructions: "Answer tersely." })
const result = await run(agent, "Reply with exactly: pong")Tool calls
The SDK runs the tool and makes the follow-up request itself.
ts
const getWeather = tool({
name: "get_weather",
description: "Current weather for a city",
parameters: z.object({ city: z.string().describe("City name") }),
execute: async ({ city }) => {
cities.push(city)
return { temp_c: 18, sky: "cloudy" }
},
})
const agent = new Agent({ name: "weather", model: modelFor(client, model), instructions: "Use the tool to answer.", tools: [getWeather] })
const result = await run(agent, PROMPT)Streaming
run(agent, input, { stream: true }) yields raw_model_stream_event items; text arrives as output_text_delta.
ts
const agent = new Agent({ name: "pong", model: modelFor(client, model), instructions: "Answer tersely." })
const stream = await run(agent, "Reply with exactly: pong", { stream: true })
let text = ""
let status: unknown
for await (const event of stream) {
if (event.type === "raw_model_stream_event" && event.data.type === "output_text_delta") text += event.data.delta
// The streamed response's status comes on its last event, not on the rawResponses entry.
if (event.type === "raw_model_stream_event" && event.data.type === "response_done") status = event.data.response.providerData?.status
}Images
ts
// Any data:image/png;base64 URL works. This is a 32px red PNG (examples/_shared/red32.dataurl.txt).
const red = readFileSync(new URL("../_shared/red32.dataurl.txt", import.meta.url), "utf8").trim()
const agent = new Agent({ name: "color", model: modelFor(client, model), instructions: "Answer in one word." })
const result = await run(agent, [
{
role: "user",
content: [
{ type: "input_text", text: "What color is this image? One word." },
{ type: "input_image", image: red },
],
},
])Structured output
outputType takes a zod schema:
ts
const Meal = z.object({ calories: z.number(), protein_g: z.number() })
const agent = new Agent({ name: "meal", model: modelFor(client, model), instructions: "Estimate the meal.", outputType: Meal })
const result = await run(agent, "Estimate: 2 boiled eggs and a slice of toast.")Limits
- A wrong key throws
AuthenticationError(401) and an unknown modelNotFoundError(404). - The SDK doesn't expose the wire
finish_reason, and has no model-list call.