Discover / Token & Cost Optimization

Token.js

by token-jsTypeScript

Unified TypeScript SDK for calling over a hundred LLMs through one API.

Toolexperimental

Maturity: experimental because latest release v0.7.1 is pre 1.0. Derived from release and commit history, not a rating.

Stars
310
Forks
40
Downloads / mo
3.7k
Last commit
2026-07-05
License
MIT
Open issues
2

Market and trust evidence

Edition not yet matched

No exact skills.sh identity match is available for this repository. Repository adoption and freshness remain visible above; install momentum is not inferred.

Trust analysis is a screening signal, not a security warranty. Read the ranking and trust methodology.

In practice

Written by AI from this repository’s README · high confidence

Supporting several model providers means learning and maintaining a different client SDK for each one.

Use it when

Use it when a TypeScript app must switch providers or models by changing two fields in the request.

Not the right pick when

Feature support varies per provider, so streaming, JSON output or tools may be missing on some models.

Capabilities

  • OpenAI format calls across AI21, Anthropic, Bedrock, Cohere, Gemini, Groq, Mistral and more
  • tools, JSON outputs, image inputs and streaming
  • runs client side with no proxy server needed
  • credentials configured per provider through environment variables
  • extendModelList to register custom or new model names
  • per provider feature compatibility table

Requirements

  • An API key per provider, set as environment variables such as OPENAI_API_KEY

Cost: Free and open source

Install

Derived from the published package name in the repository, not from a model.

Video walkthroughs

Third-party YouTube uploads matched to this tool by title, channel and repository name on 2026-08-03. Not made, reviewed or endorsed by SkillPilot. View counts and publish months are as of the match date and the month is approximate. Nothing loads from YouTube until you press play.

What the repository ships

Has testsHas docsCI configured

Detected from the actual files in the repository root.

Latest release v0.7.1

Published 2025-04-21

Patch Changes

  • 2fc6edd: Fix: resolve error where new anthropic models do not work due to hardcoded max token limit

Tags

README

Token.js

Integrate 200+ LLMs with one TypeScript SDK using OpenAI's format. Free and open source. No proxy server required.

Features

  • Use OpenAI's format to call 200+ LLMs from 10+ providers.
  • Supports tools, JSON outputs, image inputs, streaming, and more.
  • Runs completely on the client side. No proxy server needed.
  • Free and open source under MIT.

Supported Providers

  • AI21
  • Anthropic
  • AWS Bedrock
  • Cohere
  • Gemini
  • Groq
  • Mistral
  • OpenAI
  • Perplexity
  • OpenRouter
  • Requesty
  • Any other model provider with an OpenAI compatible API

Documentation

Setup

Installation


npm install token.js

Usage

Import the Token.js client and call the create function with a prompt in OpenAI's format. Specify the model and LLM provider using their respective fields.


OPENAI_API_KEY=<openai api key>

import { TokenJS } from 'token.js'

// Create the Token.js client
const tokenjs = new TokenJS()

async function main() {
  // Create a model response
  const completion = await tokenjs.chat.completions.create({
    // Specify the provider and model
    provider: 'openai',
    model: 'gpt-4o',
    // Define your message
    messages: [
      {
        role: 'user',
        content: 'Hello!',
      },
    ],
  })
  console.log(completion.choices[0])
}
main()

Access Credentials

We recommend using environment variables to configure the credentials for each LLM provider.


# OpenAI
OPENAI_API_KEY=
# AI21
AI21_API_KEY=
# Anthropic
ANTHROPIC_API_KEY=
# Cohere
COHERE_API_KEY=
# Gemini
GEMINI_API_KEY=
# Groq
GROQ_API_KEY=
# Mistral
MISTRAL_API_KEY=
# Perplexity
PERPLEXITY_API_KEY=
# OpenRouter
OPENROUTER_API_KEY=
# Requesty
REQUESTY_API_KEY=
# AWS Bedrock
AWS_REGION_NAME=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
# OpenAI Compatible
OPENAI_COMPATIBLE_API_KEY=

Streaming

Token.js supports streaming responses for all providers that offer it.


import { TokenJS } from 'token.js'

const tokenjs = new TokenJS()

async function main() {
  const result = await tokenjs.chat.completions.create({
    stream: true,
    provider: 'openai',
    model: 'gpt-4o',
    messages: [
      {
        role: 'user',
        content: `Tell me about yourself.`,
      },
    ],
  })

  for await (const part of result) {
    process.stdout.write(part.choices[0]?.delta?.content || '')
  }
}
main()

Function Calling

Token.js supports the function calling tool for all providers and models that offer it.


import { TokenJS, ChatCompletionTool } from 'token.js'

const tokenjs = new TokenJS()

async function main() {
  const tools: ChatCompletionTool[] = [
    {
      type: 'function',
      function: {
        name: 'get_current_weather',
        description: 'Get the current weather in a given location',
        parameters: {
          type: 'object',
          properties: {
            location: {
              type: 'string',
              description: 'The city and state, e.g. San Francisco, CA',
            },
          },
          required: ['location'],
        },
      },
    },
  ]

  const result = await tokenjs.chat.completions.create({
    provider: 'gemini',
    model: 'gemini-1.5-pro',
    messages: [
      {
        role: 'user',
        content: `What's the weather like in San Francisco?`,
      },
    ],
    tools,
    tool_choice: 'auto',
  })

  console.log(result.choices[0].message.tool_calls)
}
main()

Extending Model Support

Token.js allows you to extend the predefined model list using the extendModelList method. Here are some example scenarios where this is useful:

  1. Adding AWS Bedrock models with regional prefixes like us.anthropic.claude-3-sonnet
  2. Supporting new model versions before they're added to the predefined list
  3. Using custom model deployments with unique names
  4. Adding experimental or beta models during testing

import { TokenJS } from 'token.js'

// Example in 2 steps: Adding AWS Bedrock Claude models with region prefix
const tokenjs = new TokenJS();
// Step 1: Register the new model name
tokenjs.extendModelList(
  "bedrock",
  'us.anthropic.claude-3-5-sonnet-20241022-v2:0',
  "anthropic.claude-3-sonnet-20240229-v1:0" // Copy features from existing model
);

// Step 2: Using the extended model in a chat completion
const result = await tokenjs.chat.completions.create({
  stream: true,
  provider: 'bedrock',
  model: 'us.anthropic.claude-3-5-sonnet-20241022-v2:0' as any, // Type casting as 'any' required
  messages: [
    {
      role: 'user',
      content: 'Tell me about yourself.',
    },
  ],
});

Note: When using extended models, type casting (as any) is required

The featureSupport parameter can be either:

  • A string matching an existing model name from the same provider to copy its feature support
  • An object specifying which features the model supports:

| Feature | Type | Description |

|------------|---------|----------------------------------------------|

| streaming | boolean | Whether the model supports streaming responses|

| json | boolean | Whether the model supports JSON mode |

| toolCalls | boolean | Whether the model supports function calling |

| images | boolean | Whether the model supports image inputs |

Feature Compatibility

This table provides an overview of the features that Token.js supports from each LLM provider.

| Provider | Chat Completion | Streaming | Function Calling Tool | JSON Output | Image Input |

| ---------- | -------------------- | -------------------- | -------------------- | -------------------- | -------------------- |

| OpenAI | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| Anthropic | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| Bedrock | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| Mistral | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :heavy_minus_sign: |

| Cohere | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :heavy_minus_sign: | :heavy_minus_sign: |

| AI21 | :white\_check\_mark: | :white\_check\_mark: | :heavy_minus_sign: | :heavy_minus_sign: | :heavy_minus_sign: |

| Gemini | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| Groq | :white\_check\_mark: | :white\_check\_mark: | :heavy_minus_sign: | :white\_check\_mark: | :heavy_minus_sign: |

| Perplexity | :white\_check\_mark: | :white\_check\_mark: | :heavy_minus_sign: | :heavy_minus_sign: | :heavy_minus_sign: |

| OpenRouter | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| Requesty | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

| OpenAI Compatible | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: | :white\_check\_mark: |

Legend

| Symbol | Description |

|--------------------|---------------------------------------|

| :white_check_mark: | Supported by Token.js |

| :heavy_minus_sign: | Not supported by the LLM provider, so Token.js cannot support it |

Note: Certain LLMs, particularly older or weaker models, do not support some features in this table. For details about these restrictions, see our LLM provider documentation.

Contributing

See our Contributing guide to learn how to contribute to Token.js.

Issues

Please let us know if there's any way that we can improve Token.js by opening an issue!

License

Token.js is free and open source software licensed under MIT.

Related tools