AG2B

OpenAI

Synchronous Stream Reasoning

OpenAiProvider connects the agent to any OpenAI-compatible /v1/chat/completions API and maps to AG2B's normalized messages.

Usage

import { Agent, OpenAiProvider } from '@ag2b/core';

const agent = new Agent({
  provider: new OpenAiProvider({
    baseURL: '/api/llm',
    model: 'gpt-4o',
  }),
});

Reasoning

Pass reasoningField to choose which property the provider reads reasoning text from. OpenAI-compatible providers don't agree on the name — DeepSeek-R1 / DeepSeek-V3 use reasoning_content (the default), OpenRouter uses reasoning.

new OpenAiProvider({
  baseURL: '/api/llm',
  model: 'openrouter/deepseek-r1',
  reasoningField: 'reasoning',
});

The provider reads the configured field off choice.message (sync) and choice.delta (stream). If your backend emits reasoning under a different name, set reasoningField accordingly — otherwise reasoning text silently drops.

Configuration

baseURL

baseURL: string

URL the provider POSTs to.

fetch

fetch: typeof fetch | undefined

Never embed API keys in the client

The provider runs in the user's browser — anything you put into baseURL, fetch headers, or environment-substituted strings ships in your bundle to every user.

Treat your LLM API key like a database password: keep it on a server, point baseURL at your own proxy, and let the proxy attach auth on the way out.

Custom fetch implementation. Defaults to globalThis.fetch. See Custom Fetch for a wrapper example.

model

model: string | undefined

Model name sent in the request body's model field (e.g. 'gpt-4o'). When omitted, no model field is sent.
A proxy endpoint may inject it. Direct calls to most OpenAI-compatible APIs will be rejected.

reasoningField

reasoningField: string | undefined

Property name on choice.message and choice.delta to read as reasoning text. Defaults to 'reasoning_content'. See Reasoning.

On this page