Usage guide
Setup, environment variables, and Python/JavaScript/Go usage examples.
OpenAILLM
OpenAI chat/completions LLM provider with streaming and tool-calling support. Wraps OpenAI’s GPT model families behind the SDK’sLLM contract. Note the
two parameter regimes: gpt-5.x reasoning models are tuned via
reasoning_effort and verbosity, while gpt-4.x non-reasoning models
are tuned via temperature and top_p.
Constructor
OpenAI API key. Falls back to the
OPENAI_API_KEY environment variable when omitted.Model id or
OpenAILLMModel. Defaults to 'gpt-5.4-nano'. Use a gpt-5.x family model for reasoning workloads or a gpt-4.x family model for classic completions.Sampling temperature (
0–2); higher is more random, lower more deterministic. Defaults to 0.7. Adjust this or top_p, not both. Applies to non-reasoning (gpt-4.x) models.Upper bound on generated tokens, including reasoning tokens for reasoning models. Defaults to
1024. The legacy max_completion_tokens kwarg is accepted and mapped here.Nucleus-sampling probability mass (
0–1); the model considers only the top top_p of token probability. Defaults to None. Adjust this or temperature, not both.-2.0–2.0; positive values discourage verbatim repetition. Defaults to None.-2.0–2.0; positive values encourage new topics. Defaults to None.Best-effort deterministic sampling seed (beta). Defaults to
None.response_format
Output format spec, e.g.
{"type": "json_object"} or a json_schema for Structured Outputs. Defaults to None (text).tool_choice
Tool-selection control:
"none", "auto", "required", or a specific {"type": "function", ...} spec. Defaults to None.Whether to allow parallel function calls. Defaults to
None (provider default of enabled).Up to 4 stop sequences at which generation halts. Defaults to
None.Stable end-user identifier for abuse monitoring (hash to avoid PII). Defaults to
None.Reasoning compute budget for
gpt-5.x reasoning models. Common values: 'minimal', 'low', 'medium', 'high' (model-dependent; 'none' requires gpt-5.1 or later). Defaults to None.Response verbosity for reasoning models:
'low', 'medium', or 'high'. Defaults to None.Whether to stream the response incrementally. Defaults to
False.Optional WebSocket endpoint override. Defaults to
None.Whether OpenAI may store the request/response for later retrieval. Defaults to
True.aclose
cancel_current_generation
chat
The conversation history to send to the model.
Optional tool definitions the model may call.
Optional conversational graph guiding the exchange.
LLMResponse: Response chunks produced as the model generates output.
emit
The event to emit.
off
The event the handler was registered for.
The handler to remove.
on
callback is omitted.
The event to listen for.
The handler to invoke when the event is emitted. If
None, a decorator is returned that registers the decorated function.The registered callback when
callback is provided, otherwise a decorator that registers and returns the function it wraps.