Skip to content

moeru-ai/xsai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

713 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

xsAI

npm version npm downloads bundle size install size license

extra-small AI SDK.

OpenAI-compatible runtime for browser, edge, and agent tooling.

Why xsAI?

xsAI is built for cases where full-featured AI frameworks are too heavy, too broad, or simply unnecessary.

It focuses on three things:

  • small size
  • runtime portability
  • a focused OpenAI-compatible surface

That means:

  • no universal provider abstraction
  • no attempt to be a full AI application framework
  • no unnecessary runtime baggage

If you want a small foundation for OpenAI-compatible apps and agents, xsAI is for you.

Install

You can also install only some of the utils of xsAI, such as @xsai/generate-text and @xsai/stream-text.

# npm
npm install xsai

# yarn
yarn add xsai

# pnpm
pnpm add xsai

# bun
bun install xsai

# deno
deno install npm:xsai

Quick Example

import { env } from 'node:process'

import { generateText } from 'xsai'

const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [
    {
      content: 'You are a helpful assistant.',
      role: 'system',
    },
    {
      content: 'This is a test, so please answer \'YES\' and nothing else.',
      role: 'user',
    },
  ],
  model: 'gpt-4o',
})

// "YES"
console.log(text)

Why It's Small

xsAI stays small by building directly on top of the Fetch API, staying ESM-only, and avoiding extra dependencies unless they are strictly necessary.

You can compare xsAI with Packagephobia and Bundlephobia:

In the following table, we used packagephobia's install size and bundlephobia's minified/gzipped size.

Package Install size Bundled size Gzipped size
xsai@0.4.0 142KB 22.7KB 7.1KB
ai@6.0.11 5740KB 301.5KB 74.3KB

xsAI reduces the install size 40x and the bundled size 13x.

Notably, this contains dependencies introduced to support tool calls and structured output.

If you only need the basic generateText, @xsai/generate-text@0.4.0 is only 22.6KB install size and 4KB bundled size (1.7KB gzipped).

Runtime Support

xsAI doesn't depend on Node.js Built-in Modules, it works well in Browsers, Deno, Bun and even the Edge Runtime.

More Examples

Streaming Text
import { env } from 'node:process'

import { streamText } from '@xsai/stream-text'

const { textStream } = streamText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  messages: [
    {
      content: 'You are a helpful assistant.',
      role: 'system',
    },
    {
      content: 'This is a test, so please answer \'The quick brown fox jumps over the lazy dog.\' and nothing else.',
      role: 'user',
    },
  ],
  model: 'gpt-4o',
})

const text: string[] = []

for await (const textPart of textStream) {
  text.push(textPart)
}

// "The quick brown fox jumps over the lazy dog."
console.log(text)
Generating Text with Tool Calling
import { env } from 'node:process'

import { generateText } from '@xsai/generate-text'
import { tool } from '@xsai/tool'
import { description, object, pipe, string } from 'valibot'

const weather = await tool({
  description: 'Get the weather in a location',
  execute: ({ location }) => JSON.stringify({
    location,
    temperature: 42,
  }),
  name: 'weather',
  parameters: object({
    location: pipe(
      string(),
      description('The location to get the weather for'),
    ),
  }),
})

const { text } = await generateText({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  maxSteps: 2,
  messages: [
    {
      content: 'You are a helpful assistant.',
      role: 'system',
    },
    {
      content: 'What is the weather in San Francisco? do not answer anything else.',
      role: 'user',
    },
  ],
  model: 'gpt-4o',
  toolChoice: 'required',
  tools: [weather],
})

// "In San Francisco, it's currently 42Β°F."
console.log(text)

Documentation

Read the documentation at xsai.js.org/docs.

Ecosystem

Agent Skills

Install the xsAI Skill to your AI coding agent:

npx skills add moeru-ai/xsai

Community Projects

xsAI is used in community and in-house projects including:

License

MIT

Sponsors

sponsors

About

πŸ€–πŸ’¬ extra-small AI SDK.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors