Overview
Thegenerate() function generates a single response from a chat model. It’s the core function for synchronous text generation with support for tool calls, reasoning, and structured conversations.
Function Signature
Parameters
The chat model instance to use for generation.
Array of messages in the conversation. Must not be empty.
Configuration for extended thinking/reasoning capabilities.
Object mapping tool names to tool definitions. Enables the model to call functions.
Controls how the model uses tools:
'auto'- Model decides whether to use tools'none'- Model won’t use tools'required'- Model must use a tool{ type: 'tool', toolName: string }- Force specific tool
Model configuration parameters.
Provider-specific options that are passed through to the underlying model.
AbortSignal for cancelling the request.
Return Value
Returns aPromise<GenerateResult> with the following properties:
Array of content parts in the response (text, reasoning, tool calls).
Concatenated text content from all text parts.
null if no text was generated.Concatenated reasoning content.
null if no reasoning was generated.Array of tool calls made by the model.
Why generation stopped:
'stop', 'length', 'tool-calls', 'content-filter', or 'unknown'.Token usage statistics including input/output tokens and cache details.
Examples
Basic Text Generation
With Configuration
With Tools
Multi-turn Conversation
Error Handling
ThrowsLLMError if:
- Messages array is empty
- Model encounters an error during generation
Source Location
~/workspace/source/packages/core-ai/src/generate.ts:12