OpenAI

Configure OpenAI chat, Responses, reasoning parameters, and media APIs.

OpenAI

Chat, Responses, reasoning and media

Use OpenAI with the generation helpers or the Copilot Runtime. For the current beta, see reasoning parameters and fallback, Responses and caching, image tool results, and speech/transcription.

Decisions, realtime voice and image generation are next-beta additions. Check release availability before using those exports.


Setup

1. Install Packages

npm install @yourgpt/copilot-sdk @yourgpt/llm-sdk openai

2. Get API Key

Get your API key from platform.openai.com

3. Add Environment Variable

.env.local
OPENAI_API_KEY=sk-...

4. Usage

import { generateText } from "@yourgpt/llm-sdk";
import { openai } from "@yourgpt/llm-sdk/openai";

const result = await generateText({
  model: openai("gpt-4o"),
  prompt: "Explain quantum computing simply.",
});

console.log(result.text);

5. Streaming (API Route)

app/api/chat/route.ts
import { streamText } from "@yourgpt/llm-sdk";
import { openai } from "@yourgpt/llm-sdk/openai";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const result = await streamText({
    model: openai("gpt-4o"),
    system: "You are a helpful assistant.",
    messages,
  });

  return result.toTextStreamResponse();
}

Model selection

The examples use specific model IDs; they are not a ranking of current models. Choose a model available to your account from the OpenAI model catalog, and verify its supported parameters and transport. See the beta upgrade guide for the released SDK versions and tested scope.


Configuration Options

import { openai } from "@yourgpt/llm-sdk/openai";

// Custom API key
const model = openai("gpt-4o", {
  apiKey: "sk-custom-key",
});

// With generation options
const result = await generateText({
  model: openai("gpt-4o"),
  prompt: "Hello",
  temperature: 0.7, // Availability depends on model and reasoning settings
  maxTokens: 4096, // Max response length
});

Tool Calling

OpenAI has excellent tool/function calling support:

import { generateText, tool } from "@yourgpt/llm-sdk";
import { openai } from "@yourgpt/llm-sdk/openai";
import { z } from "zod";

const result = await generateText({
  model: openai("gpt-4o"),
  prompt: "What is the weather in Tokyo?",
  tools: {
    getWeather: tool({
      description: "Get weather for a city",
      parameters: z.object({
        city: z.string().describe("City name"),
      }),
      execute: async ({ city }) => {
        // Your weather API call
        return { temperature: 22, condition: "sunny" };
      },
    }),
  },
  maxSteps: 5, // Allow multiple tool calls
});

Vision (Image Input)

GPT-4o supports image analysis. Send images in your messages:

const result = await generateText({
  model: openai("gpt-4o"),
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "What's in this image?" },
        { type: "image", image: base64ImageData },
      ],
    },
  ],
});

With Copilot UI

Use with the Copilot React components:

app/providers.tsx
"use client";

import { CopilotProvider } from "@yourgpt/copilot-sdk/react";

export function Providers({ children }: { children: React.ReactNode }) {
  return <CopilotProvider runtimeUrl="/api/chat">{children}</CopilotProvider>;
}

Pricing

Use OpenAI API pricing for current rates. Provider metadata and normalized token totals have different scopes; cache-aware usage and pricing are still pending.


Reasoning model parameters

The beta recognizes GPT-5/GPT-6 and o-series reasoning families. When reasoning is active, it omits unsupported temperature values and emits a warning. A request such as temperature: 0 no longer relies on treating GPT-6 Luna as a legacy sampling model.

The SDK does not disable reasoning automatically to preserve temperature. If your model supports non-reasoning mode and you explicitly want it:

import { generateText } from "@yourgpt/llm-sdk";
import { openai } from "@yourgpt/llm-sdk/openai";

const result = await generateText({
  model: openai("gpt-6-luna", {
    reasoningEffort: "none",
    onWarning: (message) => console.warn(message),
  }),
  prompt: "Classify this message: I need help changing my password.",
  temperature: 0,
  maxTokens: 128,
});

Unsupported reasoning-effort combinations fail before a request. maxTokens maps to max_completion_tokens for reasoning Chat requests, max_tokens for legacy Chat, and max_output_tokens for Responses.

OpenAI-compatible servers with different parameter semantics can opt out with provider option parameterPolicy: 'passthrough'; this gives your application responsibility for compatible parameters. onWarning lets you observe normalization. See compatibility fallback for advancing past a narrowly identified parameter error without retrying the same invalid request.


Responses, image tools, and audio

Use api: 'responses' to select Responses explicitly. The default api: 'auto' preserves compatibility routing, including Chat defaults for custom endpoints. See the Responses guide for stored/stateless continuation and the cache options supported by this beta.

Native image tool results require Responses on OpenAI; ordinary image inputs in user messages are a separate feature. Speech generation and transcription use separate audio model factories. Native Decisions and realtime calls remain pending.


Next Steps

On this page