Quickstart
Get up and running with core-ai in just a few minutes. This guide shows you how to create a simple chat completion using OpenAI’s GPT model.Prerequisites
Before you begin, make sure you have:- Node.js 22 or higher installed
- An OpenAI API key (get one here)
- Completed the installation steps
Create your first chat completion
1
Set up your project
Create a new file called Make sure you have your API key set as an environment variable:
chat.ts in your project:2
Import dependencies
Add the necessary imports to your The
chat.ts file:chat.ts
generate function handles chat completions, while createOpenAI initializes the OpenAI provider.3
Initialize the provider and model
Create an OpenAI provider instance and select a chat model:
chat.ts
You can use any OpenAI model ID like
gpt-5.4, gpt-5-mini, gpt-5, or o3-mini.4
Generate a response
Call the
generate function with your model and messages:chat.ts
5
Run your code
Execute your script using You should see the AI’s response printed to the console along with token usage statistics.
tsx:Complete example
Here’s the full working example:chat.ts
Try streaming
core-ai makes streaming responses just as easy. Here’s how to stream text as it’s generated:streaming.ts
Switch providers
One of core-ai’s key features is provider portability. Switch from OpenAI to Anthropic with just two lines:Next steps
Now that you have a working chat completion, explore more advanced features:Structured outputs
Generate type-safe JSON objects with Zod schemas
Tool calling
Let AI call functions with validated parameters
Embeddings
Generate vector embeddings for semantic search
Image generation
Create images from text prompts