API Reference

TokenSense is a transparent proxy — you call it the same way you'd call your AI provider directly. This page covers the endpoint format, authentication, and code examples.

Base URL

Your TokenSense endpoint is shown on your dashboard home page. All requests go through this URL:

https://api.tokensense.io/chat/completions

Replace /chat/completions with the appropriate path for your provider and model type.

Supported endpoints

EndpointMethodDescription
/v1/chat/completionsPOSTChat completions (OpenAI format, all providers)
/v1/messagesPOSTNative Anthropic Messages API
/v1beta/models/{model}:{action}POSTNative Gemini API (generateContent, streamGenerateContent)
/v1/embeddingsPOSTText embeddings (OpenAI, xAI, Mistral)
/v1/images/generationsPOSTImage generation (OpenAI, Google Imagen, fal Flux)
/v1/audio/speechPOSTText-to-speech (OpenAI)
/v1/audio/transcriptionsPOSTAudio transcription (OpenAI Whisper)
/v1/responsesPOSTOpenAI Responses API — agentic endpoint (web search, file search, code interpreter, image generation, MCP tools, stateful sessions). Native passthrough for OpenAI; automatic translation for other providers.
/v1/modelsGETList available models
/healthGETHealth check (no auth required)

The most common endpoint is /v1/chat/completions — it accepts OpenAI-format requests and automatically translates for Anthropic, Gemini, xAI, and Mistral. Use the native endpoints (/v1/messages, /v1beta/models/...) when you need provider-specific features that don't map to the OpenAI format.

Authentication

Every request must include a Bearer token with your TokenSense API key:

Authorization: Bearer YOUR_TOKENSENSE_KEY

Your TokenSense API key is not your provider key. You add provider keys separately in Settings → Providers. TokenSense uses them to forward requests on your behalf.

Supported formats

TokenSense detects the provider from the model name in your request and routes automatically. You can send requests in any of these formats:

  • OpenAI format/v1/chat/completions with any model. TokenSense translates automatically for all providers.
  • Native Anthropic/v1/messages for the Anthropic Messages API (passthrough, no translation).
  • Native Gemini/v1beta/models/{model}:{action} for the Gemini API (passthrough, no translation).

xAI and Mistral are natively OpenAI-compatible — requests pass through directly via /v1/chat/completions with zero translation.

OpenAI Responses API

The Responses API is OpenAI's newer agentic endpoint, and it's what powers tools like the OpenAI Agents SDK and Vercel AI SDK. TokenSense now supports it at /v1/responses, so any tool that uses the Responses API can run through your TokenSense endpoint with full cost tracking on every request.

With OpenAI models, everything works as a native passthrough. Built-in tools (web search, file search, code interpreter, image generation), remote MCP tools, and stateful sessions (using store or previous_response_id) all work unchanged, and token usage is tracked on every request.

Note on built-in tool fees: OpenAI charges separate per-use fees for built-in tools (web search, file search, code interpreter, image generation) on top of token usage. Those tool fees are not yet includedin TokenSense's cost figures — calls that use them are flagged in your logs, and budgets currently count token cost only. Account for built-in tool fees separately until per-tool pricing lands.

With other providers (Anthropic, Gemini, xAI, Mistral), TokenSense automatically translates standard requests and function/tool calls so they work through the same /v1/responsesendpoint. However, OpenAI-only features like built-in tools, MCP, and stateful storage aren't available on other providers — if you try to use them, you'll get a clear error message explaining why.

Tip: If a tool lets you choose between the Responses API and Chat Completions and you run into issues on a non-OpenAI model, switch it to Chat Completions. For example, Twenty CRM uses OPENAI_COMPATIBLE_API_TYPE=chat for this.

Attribution headers

Add these optional headers to track costs per workflow and step in your dashboard:

HeaderPurposeExample
x-workflow-tagIdentifies the workflowlead-enrichment
x-stepIdentifies the step within a workflowsummarize
x-execution-idGroups requests from the same runexec-12345
x-sourceIdentifies the tool or platformmake
x-projectAssociates with a projectclient-a

Response format

TokenSense returns the exact same response as the upstream provider. Your code doesn't need any changes to parse the response — it's identical to calling the provider directly.

Code examples

cURL

curl https://api.tokensense.io/chat/completions \
  -H "Authorization: Bearer YOUR_TOKENSENSE_KEY" \
  -H "Content-Type: application/json" \
  -H "x-workflow-tag: my-workflow" \
  -d '{
    "model": "gpt-5-mini",
    "messages": [
      {"role": "user", "content": "Hello, world!"}
    ]
  }'

Python

import requests

response = requests.post(
    "https://api.tokensense.io/chat/completions",
    headers={
        "Authorization": "Bearer YOUR_TOKENSENSE_KEY",
        "Content-Type": "application/json",
        "x-workflow-tag": "my-workflow",
    },
    json={
        "model": "gpt-5-mini",
        "messages": [
            {"role": "user", "content": "Hello, world!"}
        ],
    },
)

print(response.json())

Node.js

const response = await fetch(
  "https://api.tokensense.io/chat/completions",
  {
    method: "POST",
    headers: {
      "Authorization": "Bearer YOUR_TOKENSENSE_KEY",
      "Content-Type": "application/json",
      "x-workflow-tag": "my-workflow",
    },
    body: JSON.stringify({
      model: "gpt-5-mini",
      messages: [
        { role: "user", content: "Hello, world!" },
      ],
    }),
  }
);

const data = await response.json();
console.log(data);

Error responses

CodeMeaningFix
400Invalid request body or missing fieldsCheck your request payload format
401Invalid or missing API keyCheck key in dashboard
402Budget exceeded or subscription lockedIncrease budget or update billing
403Missing provider key or routing policy blockAdd provider key in Settings → Providers
404Model not found in catalogCheck model name, use /v1/models to list
429Rate limit exceeded (tiered by plan)Check Retry-After header, slow down
502Upstream provider errorRetry with backoff
504Provider timeoutTry a faster model

See the full Error Codes reference for detailed troubleshooting.

Related