Skip to main content

Installation (TypeScript)

LLM Context Forge is published on npm and provides native support for Node.js (18+), Edge environments, and the browser.

Package Managers

Installs the package and its single runtime dependency (js-tiktoken).

# npm
npm install llm-context-forge

# yarn
yarn add llm-context-forge

# pnpm
pnpm add llm-context-forge

Module Systems

The package supports both ECMAScript Modules (ESM) and CommonJS (CJS) out of the box. Types are included directly in the package via the exports configuration in package.json.

ESM (Recommended)

import { TokenCounter, ContextWindow } from 'llm-context-forge';

const counter = new TokenCounter('gpt-4o');

CommonJS

const { TokenCounter, ContextWindow } = require('llm-context-forge');

const counter = new TokenCounter('gpt-4o');

Environment Compatibility

  • Node.js: Full support (v18+)
  • Edge Runtimes: Full support (Vercel Edge, Cloudflare Workers, Deno)
  • Browser: Full support (Webpack, Vite, Rollup)

Note: In the browser, the package utilizes js-tiktoken which lazily loads BPE encodings. You will need a bundler that handles fetch requests for .tiktoken files or configure your bundler to statically include them.