Skip to main content

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:

Create your first chat completion

1

Set up your project

Create a new file called chat.ts in your project:
Make sure you have your API key set as an environment variable:
2

Import dependencies

Add the necessary imports to your chat.ts file:
chat.ts
The 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 tsx:
You should see the AI’s response printed to the console along with token usage statistics.

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
The .result property returns a promise that resolves with the aggregated GenerateResult once the stream completes. Streams are replayable — you can iterate multiple times.

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