Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

839 B
Raw Blame
/**
 * Approximate token count for a tool call's input or output, using the
 * ~4-chars-per-token heuristic the MCP performance dashboard tracks. The server
 * does not capture LLM-observability spans, so token usage is estimated from the
 * serialized size of the value rather than read from a generation/trace payload.
 */
export function estimateTokens(value: unknown): number {
    if (value === undefined || value === null) {
        return 0
    }
    let text: string
    if (typeof value === 'string') {
        text = value
    } else {
        try {
            text = JSON.stringify(value) ?? ''
        } catch {
            // Non-serializable value (circular refs, BigInt, ...) — never break
            // the request for an analytics estimate.
            return 0
        }
    }
    return Math.ceil(text.length / 4)
}