LogoAI SDK Token Usage

Overview

AI SDK Token Usage is a lightweight TypeScript library designed to help developers accurately measure and visualize token usage across different AI model providers. The library is targeted for developers using AI SDK with React.

Whether you’re integrating OpenAI, Anthropic, Google, or other providers, the library abstracts away provider-specific differences so you can focus on building AI-powered applications with clear insights into cost and context window utilization.

The open-source model database models.dev is used to retrieve up-to-date pricing and limits, with plans to support more registries soon such as OpenRouter.

If you learn best from seeing things in action, check out the Demo App — an example project that demonstrates how AI SDK Token Usage integrates with AI SDK to track tokens, estimate costs, and visualize token usage in real time.

Demo appDemo app

Example view from the demo app, showing real-time token usage visualization.

Installing

npm install ai-sdk-token-usage

AI SDK Token Usage requires AI SDK v5, read the official migration guide to migrate from v4 to v5.


Features

Context Window

Get accurate token usage to determine remaining context window with the hook useTokenContext.

Cost Estimates

Get cost estimates for either a single message or your entire chat with the hook useTokenCost.

Model Details

Get model details such as token limits and pricing with the hook useModelDetails.

Formatting

Provides ulitity methods to format price and token amount. Check out Formatting.

On this page