export function getNonCachedInputTokens(usage) {
    if (typeof usage.inputTokenDetails?.noCacheTokens === "number") {
        return usage.inputTokenDetails.noCacheTokens;
    }
    if (typeof usage.totalTokens === "number" &&
        typeof usage.outputTokens === "number" &&
        typeof usage.inputTokens === "number" &&
        usage.totalTokens - usage.outputTokens === usage.inputTokens) {
        return Math.max(0, usage.inputTokens - (usage.cachedInputTokens ?? 0));
    }
    return usage.inputTokens ?? 0;
}
export function getTotalInputTokens(usage) {
    const nonCached = getNonCachedInputTokens(usage);
    const cacheRead = usage.cachedInputTokens ?? usage.inputTokenDetails?.cacheReadTokens ?? 0;
    const cacheWrite = usage.inputTokenDetails?.cacheWriteTokens ?? 0;
    const hasDetailedBreakdown = usage.inputTokenDetails?.noCacheTokens !== undefined ||
        usage.inputTokenDetails?.cacheReadTokens !== undefined ||
        usage.inputTokenDetails?.cacheWriteTokens !== undefined;
    if (hasDetailedBreakdown) {
        return nonCached + cacheRead + cacheWrite;
    }
    if (typeof usage.totalTokens === "number" &&
        typeof usage.outputTokens === "number") {
        return Math.max(0, usage.totalTokens - usage.outputTokens);
    }
    if (cacheRead > 0 || cacheWrite > 0) {
        return nonCached + cacheRead + cacheWrite;
    }
    return usage.inputTokens ?? nonCached;
}
/**
 * Tokens consumed against the model's input context window, for display in the
 * UI's context-usage indicator. This is the sum of fresh, cache-read, and
 * cache-write input tokens — cache reads/writes occupy the input window just
 * like fresh input (cache is a cost discount, not a context-window discount).
 *
 * Excludes outputTokens and reasoningTokens: those do not consume the input
 * window. (Note: for Anthropic, extended-thinking tokens are bundled inside
 * `output_tokens` in the raw API response and are not separately surfaced as
 * `reasoningTokens` by @ai-sdk/anthropic, so this function's exclusion of
 * reasoning tokens is defensive for other providers.)
 */
export function computeContextUsed(usage) {
    return getTotalInputTokens(usage);
}
export function isOverflow(usage, contextLimit, outputReserve) {
    const total = getTotalTokens(usage);
    const available = contextLimit - outputReserve;
    return total > available;
}
export function getTotalTokens(usage) {
    const recomputedTotal = getTotalInputTokens(usage) + (usage.outputTokens ?? 0);
    const hasDetailedBreakdown = usage.inputTokenDetails?.noCacheTokens !== undefined ||
        usage.inputTokenDetails?.cacheReadTokens !== undefined ||
        usage.inputTokenDetails?.cacheWriteTokens !== undefined;
    if (hasDetailedBreakdown) {
        return recomputedTotal;
    }
    return usage.totalTokens ?? recomputedTotal;
}
//# sourceMappingURL=token-tracker.js.map