Skip to main content
The generate() function provides synchronous chat completion, returning a complete response from the language model.

Basic Usage

Generate a simple chat completion:

Using Different Providers

core-ai supports multiple providers with the same API:

Configuration options

Customize model behavior with configuration parameters:
temperature, maxTokens, and topP are top-level options. stopSequences, frequencyPenalty, and presencePenalty are provider-specific and passed via providerOptions. OpenAI supports them through openai.chat.chatModel() and @core-ai/openai-compat, not through Responses models. See Configuration for details.

Response Structure

The generate() function returns a GenerateResult object:

Understanding Token Usage

Multi-turn conversations

Build conversations by including previous messages. Use resultToMessage() to convert a GenerateResult into an AssistantMessage:

Error Handling

Handle errors gracefully:

Best Practices

System messages set the assistant’s behavior and context:
Control costs and response length with maxTokens:
Check why generation stopped:

Next Steps

Streaming

Stream responses in real-time for better UX

Tool Calling

Let models use tools and functions

Multi-Modal

Work with images and files

Structured Outputs

Get type-safe JSON responses