Create a new Chat instance.
Optionalconfig: ChatConfigOptional configuration
// With specific model
new Chat({ model: 'gpt-4o' });
// With model preset
new Chat({ model: 'fast' }); // Uses fastest model for default provider
new Chat({ model: 'best' }); // Uses highest quality model
new Chat({ model: 'cheap' }); // Uses most cost-effective model
new Chat({ model: 'balanced' }); // Good quality/speed/cost balance
Get current model
Set model
Get message history (readonly)
Get registered tools (readonly)
Metadata from the most recent generate() or stream() call. Includes accumulated usage, cost, and timing across all API round-trips (including tool-calling iterations). Available after any generation call completes. Returns null before any generation has occurred.
Add a system message.
Optionalmetadata: Record<string, unknown>this for chaining
Add a user message.
Optionalmetadata: Record<string, unknown>this for chaining
Add an assistant message.
Optionalmetadata: Record<string, unknown>this for chaining
Clear all messages.
this for chaining
Clear all tools.
this for chaining
Generate a text response. Automatically uses registered tools if any exist. Appends assistant response to message history. Populates lastResult with accumulated usage metadata.
Optionaloptions: GenerateWithToolsOptionsGeneration options (includes tool options when tools registered)
Generated text content
Generate a text response with full result metadata. Does NOT use tools - for direct generation only. Appends assistant response to message history. Also populates lastResult with the same result.
Optionaloptions: GenerateOptionsGeneration options
Full generation result with rich metadata (usage, timing, cost)
Stream a text response. Automatically uses registered tools if any exist. Appends assistant response to history after streaming completes. Populates lastResult with accumulated usage metadata once the generator is fully consumed.
Optionaloptions: StreamWithToolsOptionsStream options (includes tool options when tools registered)
Async generator of content strings
Stream and accumulate the full response. Convenience method that collects all chunks. Populates lastResult with accumulated usage metadata.
Optionaloptions: StreamWithToolsOptionsStream options
Full accumulated content
Generate structured data using a Schema. Appends assistant response to history.
Schema class or instance
Optionaloptions: GenerateOptionsGeneration options
Populated schema instance
Serialize to JSON object.
Serialize to JSON string.
Load state from JSON object. Replaces current messages and config.
StaticfromCreate a clone of this chat with copied state.
Fork this chat - creates a clone for branching conversations.
Chat class for unified LLM interactions.
After any generation call (
generate(),stream(),generateWithResult(),streamAccumulate()), accesschat.lastResultfor accumulated metadata including token usage, cost, timing, and iteration count across all API round-trips (including tool-calling loops).Example