Documentation

Tokoscope wraps your existing LLM client in two lines and gives you full token visibility, automatic compression, semantic caching, and cost attribution — with zero infrastructure changes.

Quick start

The fastest way to get started:

app.js
// 1. Install
// npm install tokoscope

// 2. Wrap your client
import OpenAI from 'openai'
import { wrap } from 'tokoscope'

const client = wrap(new OpenAI(), {
  apiKey: 'ts_live_...'
})

// 3. Use exactly as before
const res = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello' }]
})

That's it. Every API call is now tracked automatically. Visit your dashboard to see live data.

Installation

JavaScript / Node.js

bash
npm install tokoscope
# or
yarn add tokoscope

Python

bash
pip install tokoscope
Requirements: Node.js 18+ or Python 3.8+. No additional dependencies required.

API key

Get your API key from app.tokoscope.com/settings. It starts with ts_live_.

Store it as an environment variable — never hardcode it in your source:

.env
TOKOSCOPE_API_KEY=ts_live_...
app.js
const client = wrap(new OpenAI(), {
  apiKey: process.env.TOKOSCOPE_API_KEY
})

OpenAI

openai.js
import OpenAI from 'openai'
import { wrap } from 'tokoscope'

const client = wrap(new OpenAI(), {
  apiKey: process.env.TOKOSCOPE_API_KEY,
  userId: 'user_123' // optional
})

const res = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello' }]
})

Anthropic

anthropic.js
import Anthropic from '@anthropic-ai/sdk'
import { wrap } from 'tokoscope'

const client = wrap(new Anthropic(), {
  apiKey: process.env.TOKOSCOPE_API_KEY
})

const res = await client.messages.create({
  model: 'claude-sonnet-4-6',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello' }]
})

Gemini

gemini.js
import { GoogleGenerativeAI } from '@google/generative-ai'
import { wrap } from 'tokoscope'

const genAI = wrap(new GoogleGenerativeAI(process.env.GEMINI_KEY), {
  apiKey: process.env.TOKOSCOPE_API_KEY
})

const model = genAI.getGenerativeModel({ model: 'gemini-2.5-flash' })
const result = await model.generateContent('Hello')

Per-user tracking

Pass a userId to attribute token usage to individual end users of your app. Users appear in the Users tab with individual token usage, cost, and waste scores.

app.js
const client = wrap(new OpenAI(), {
  apiKey: process.env.TOKOSCOPE_API_KEY,
  userId: currentUser.id // any string identifier
})

Caching

Tokoscope automatically caches responses using two layers:

Cache TTL is 7 days. Clear cache anytime from Settings.

Cache hit log: When a cache hit fires, Tokoscope logs it to the console:
⚡ Tokoscope cache hit [semantic (89.3% match)] — saved 93 tokens ($0.000049)

Prompt compression

Prompts with a waste score above 30% are automatically compressed before being tracked. The compressed version is stored alongside the original and visible in the Prompts tab.

Compression uses Claude Haiku and typically reduces prompt length by 40–90% without changing model behavior.

Streaming

Streaming is fully supported across OpenAI, Anthropic, and Gemini. Set stream: true (or stream=True in Python) as usual. Tokoscope passes every chunk through unchanged and tracks token usage once the stream completes.

OpenAI

stream.js
const stream = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello' }],
  stream: true
})

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '')
}
// Tracking fires automatically once the stream ends

Anthropic

Both messages.create(stream=True) and the messages.stream() helper are supported.

anthropic-stream.py
# Option 1 — create(stream=True)
stream = client.messages.create(
  model='claude-sonnet-4-6',
  max_tokens=1024,
  messages=[{'role': 'user', 'content': 'Hello'}],
  stream=True
)
for event in stream:
  if event.type == 'content_block_delta':
    print(event.delta.text, end='', flush=True)

# Option 2 — the .stream() helper (context manager)
with client.messages.stream(
  model='claude-sonnet-4-6',
  max_tokens=1024,
  messages=[{'role': 'user', 'content': 'Hello'}]
) as stream:
  for text in stream.text_stream:
    print(text, end='', flush=True)
# Tracking fires from the final message usage

Gemini

gemini-stream.py
stream = model.generate_content(
  'Hello',
  stream=True
)
for chunk in stream:
  print(chunk.text, end='', flush=True)
# Tracking reads usage_metadata from the final chunk

Mistral

Both chat.complete() and chat.stream() are supported. Pass stream() directly — Tokoscope wraps it automatically.

mistral-stream.js
// Non-streaming
const result = await client.chat.complete({
  model: 'mistral-large-latest',
  messages: [{ role: 'user', content: 'Hello' }]
})

// Streaming
const stream = await client.chat.stream({
  model: 'mistral-large-latest',
  messages: [{ role: 'user', content: 'Hello' }]
})

for await (const chunk of stream) {
  process.stdout.write(chunk.data?.choices[0]?.delta?.content || '')
}
// Tracking fires automatically once the stream ends
Note: Streaming responses skip the cache lookup and are not cached in this version. Semantic and exact match caching only apply to non-streaming calls.

Budget alerts + webhooks

Set a monthly spend threshold in Settings. When your spend crosses the threshold, Tokoscope fires an email alert and — if configured — a webhook POST to any HTTPS endpoint. Alerts fire once per calendar month.

Setting up a Slack alert

  1. In Slack, go to Apps → Incoming Webhooks → Add to Slack
  2. Choose a channel and click Allow
  3. Copy the webhook URL (starts with https://hooks.slack.com/services/...)
  4. Paste it into the Webhook Alert field in Settings
  5. Click Send test to verify delivery

Tokoscope automatically detects Slack URLs and sends a formatted block message with spend, threshold, provider, and a link to your dashboard.

Custom webhook endpoint

Any HTTPS URL works. Tokoscope sends a POST request with a JSON payload when your budget threshold is crossed:

webhook-payload.json
{
  "event": "budget_alert",
  "userId": "usr_...",
  "email": "you@example.com",
  "budgetThreshold": 10,
  "monthlySpend": 10.0142,
  "triggeredAt": "2026-07-14T12:00:00.000Z",
  "provider": "openai",
  "model": "gpt-4o-mini"
}

Testing your webhook

Use the Send test button in Settings to fire a test payload with "test": true immediately, without waiting for your budget threshold to be crossed. The test payload uses your configured threshold value with a spend $0.01 above it.

Note: Webhook alerts fire once per calendar month — the same cadence as email alerts. If you want to re-trigger a test after clearing your alert state, use the Send test button in Settings.

wrap() options

OptionTypeRequiredDescription
apiKeystringYesYour Tokoscope API key from Settings
userIdstringNoEnd-user ID for per-user attribution

Python uses snake_case: api_key and user_id.

Supported models & pricing

ProviderModelInput (per 1K tokens)Output (per 1K tokens)
openaigpt-4o$0.005$0.015
openaigpt-4o-mini$0.00015$0.0006
openaigpt-4-turbo$0.01$0.03
anthropicclaude-opus-4-6$0.015$0.075
anthropicclaude-sonnet-4-6$0.003$0.015
anthropicclaude-haiku-4-5$0.00025$0.00125
geminigemini-2.5-flash$0.0001$0.0004
geminigemini-2.5-pro$0.00125$0.01
geminigemini-1.5-flash$0.000075$0.0003
geminigemini-1.5-pro$0.00125$0.005

VS Code Extension

The Tokoscope VS Code extension lets you see token counts, waste scores, and cost estimates for any LLM prompt directly in your editor — without leaving VS Code.

Install from VS Code Marketplace
marketplace.visualstudio.com

Features

Setup

Command Palette
# 1. Open Command Palette
Cmd+Shift+P (Mac) / Ctrl+Shift+P (Windows)

# 2. Run
Tokoscope: Set API Key

# 3. Paste your key from app.tokoscope.com/settings
ts_live_...
Tip: After installing, the token counter appears in the status bar (bottom right) whenever you have a file open. Click it to analyze the current file.

VS Code extension

Tokoscope is available as a VS Code extension. See token counts, waste scores, and cost estimates for any prompt or file — without leaving your editor.

Install: Search Tokoscope in the VS Code Extensions panel, or install from the marketplace:

VS Code Marketplace →  ·  Open VSX (Cursor, Windsurf, VSCodium) →

What it does

Setup

Command Palette
1. Cmd+Shift+P → Tokoscope: Set API Key
2. Enter your key from app.tokoscope.com/settings
3. Done — token counts appear in the status bar

Commands

CommandDescription
Tokoscope: Analyze Current FileFull file analysis with waste score and suggestions
Tokoscope: Analyze Selected TextAnalyze highlighted text only
Tokoscope: Set API KeySave your Tokoscope API key
Tokoscope: Open DashboardOpen app.tokoscope.com/dashboard

Changelog