thedotmack/claude-mem · info

High token usage detected - consider reducing context

Error message

High token usage detected - consider reducing context

What it means

A heuristic warning logged by queryOpenRouterMultiTurn when total tokens used in a multi-turn OpenRouter query exceed 50,000. It is not an error; it flags that the conversation history is large and costs/latency grow, suggesting context reduction.

Solutions

  1. Trim or summarize conversation history before it exceeds the threshold.
  2. Lower the max history length / enable context windowing in the session manager.
  3. Move large content out of the prompt (reference files instead of inlining).
  4. Monitor costUSD in the same log to decide if the usage is intentional.

Example fix

// before
messages: [...fullHistory, prompt]
// after
messages: [...fullHistory.slice(-MAX_TURNS), prompt]
Defensive patterns

Strategy: validation

Validate before calling

const estimate = history.reduce((n, m) => n + (m.content?.length ?? 0), 0) / 4;
if (estimate > 45000) history = summarize(history);

Prevention

When it happens

Trigger: After a multi-turn query completes, tokensUsed > 50000 when the usage is logged; history.length (messagesInContext) is large.

Common situations: Long-running agent sessions that never trim history; pasting large files into the prompt; models with small context windows causing truncation or cost spikes on OpenRouter.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/c6371357b0ccc88d. Report an issue: GitHub.

Appendix: source

Thrown at src/services/worker/OpenRouterProvider.ts:644

      ? data.usage.cost_details.upstream_inference_cost
      : undefined;
    const costUsd = orCost !== undefined || upstreamCost !== undefined
      ? (orCost ?? 0) + (upstreamCost ?? 0)
      : undefined;
    const servedModel = typeof data.model === 'string' && data.model ? data.model : undefined;

    if (tokensUsed) {
      logger.info('SDK', 'OpenRouter API usage', {
        model: servedModel ?? model,
        inputTokens: realInputTokens || 0,
        outputTokens: realOutputTokens || 0,
        totalTokens: tokensUsed,
        ...(costUsd !== undefined ? { costUSD: costUsd.toFixed(6) } : {}),
        messagesInContext: history.length
      });

      if (tokensUsed > 50000) {
        logger.warn('SDK', 'High token usage detected - consider reducing context', {
          totalTokens: tokensUsed,
          ...(costUsd !== undefined ? { costUSD: costUsd.toFixed(6) } : {}),
        });
      }
    }

    return { content, tokensUsed, inputTokens: realInputTokens, outputTokens: realOutputTokens, costUsd, servedModel };
  }

}

export function isOpenRouterAvailable(settingsPath: string = USER_SETTINGS_PATH): boolean {
  return Boolean(resolveOpenRouterConfig(settingsPath).apiKey);
}

export function isOpenRouterSelected(): boolean {
  const settingsPath = USER_SETTINGS_PATH;
  const settings = SettingsDefaultsManager.loadFromFile(settingsPath);

View on GitHub (pinned to d8bc9755e7)