OpenAI Usage Codec (codecs/openai.ts)

aimock · src/response/usage/codecs/openai.ts · typescript · MIT
ai, analytics, tag:tokens, tag:codec, tag:estimation

Resolves OpenAI-style token usage from overrides or estimates from prompt/completion text, preserving alias precedence. Reuse when normalizing token counts across OpenAI-compatible responses.

// Source: https://github.com/CopilotKit/aimock | License: MIT | Copyright 2025 CopilotKit
// Provided via justclaw.it.com; keep this notice with the code.
import type { ResponseOverrides } from "../../../types.js";
import { estimateTokens } from "../estimate.js";

/** Token aliases shared with the OpenRouter passthrough exclusion. Order is observable. */
export const OPENAI_TOKEN_KEYS = [
  "prompt_tokens",
  "completion_tokens",
  "total_tokens",
  "input_tokens",
  "output_tokens",
  "promptTokenCount",
  "candidatesTokenCount",
  "totalTokenCount",
] as const;

interface ResolvedOpenAIUsage {
  prompt: number;
  completion: number;
  total: number;
}

/** Preserve definedness, alias precedence and caller text estimation without sanitizing. */
export function resolve(
  overrides: Respo

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key