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)
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.
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.
Optionaloptions: GenerateOptionsGeneration options
Full generation result with rich metadata (usage, timing, cost)
Stream a text response. Appends assistant response to history after streaming completes.
Optionaloptions: StreamOptionsStream options
Async generator of content strings
Stream and accumulate the full response. Convenience method that collects all chunks.
Optionaloptions: StreamOptionsStream 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.
Example