Skip to main content
Glama
atriumn
by atriumn

calculate_estimate

Estimate token costs for any model by providing input and output token counts, with optional prompt caching discounts.

Instructions

Estimate the cost for a given number of input and output tokens on a specific model. Supports optional cached_tokens for prompt caching discounts.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
model_nameYesModel name (fuzzy matched)
input_tokensYesNumber of input tokens
output_tokensYesNumber of output tokens
cached_tokensNoNumber of input tokens served from cache (prompt caching). Must be <= input_tokens. These tokens are billed at the cached rate instead of the standard input rate.

Implementation Reference

  • src/tools.ts:22-49 (registration)
    Tool registration entry for 'calculate_estimate' including name, description, and inputSchema definition.
    {
      name: "calculate_estimate",
      description:
        "Estimate the cost for a given number of input and output tokens on a specific model. Supports optional cached_tokens for prompt caching discounts.",
      inputSchema: {
        type: "object" as const,
        properties: {
          model_name: {
            type: "string",
            description: "Model name (fuzzy matched)",
          },
          input_tokens: {
            type: "number",
            description: "Number of input tokens",
          },
          output_tokens: {
            type: "number",
            description: "Number of output tokens",
          },
          cached_tokens: {
            type: "number",
            description:
              "Number of input tokens served from cache (prompt caching). Must be <= input_tokens. These tokens are billed at the cached rate instead of the standard input rate.",
          },
        },
        required: ["model_name", "input_tokens", "output_tokens"],
      },
    },
  • Zod schema for validating calculate_estimate inputs: model_name, input_tokens, output_tokens, and optional cached_tokens.
    const calculateEstimateSchema = z.object({
      model_name: z.string().min(1),
      input_tokens: z.number().nonnegative(),
      output_tokens: z.number().nonnegative(),
      cached_tokens: z.number().nonnegative().optional(),
    });
  • Handler logic for 'calculate_estimate' that parses inputs, looks up the model, calculates costs (with tiered pricing and prompt caching support), and returns a formatted cost estimate.
    case "calculate_estimate": {
      const { model_name, input_tokens, output_tokens, cached_tokens } =
        calculateEstimateSchema.parse(args);
      const models = await getModels();
      const { entry: model, isFineTuned } = fuzzyMatchWithMetadata(model_name, models);
    
      if (!model) {
        return {
          content: [
            {
              type: "text",
              text: `No model found matching "${model_name}".`,
            },
          ],
        };
      }
    
      // Resolve cached token count: cap at input_tokens, ignore if model doesn't support caching
      const resolvedCachedTokens =
        cached_tokens != null && model.cache_read_input_token_cost != null && cached_tokens > 0
          ? Math.min(cached_tokens, input_tokens)
          : 0;
      const uncachedInputTokens = input_tokens - resolvedCachedTokens;
    
      const result = calculateTieredCost(model, uncachedInputTokens, output_tokens);
      const cachedCost =
        resolvedCachedTokens > 0
          ? resolvedCachedTokens * (model.cache_read_input_token_cost ?? 0)
          : 0;
      const totalCost = result.totalCost + cachedCost;
    
      const lines: string[] = [`Cost Estimate for ${model.key}`, ``];
    
      if (isFineTuned) {
        lines.push(
          `⚠️ Note: This estimate is for the base model (${model.key}). Fine-tuned models use the same pricing as their base model.`,
        );
        lines.push(``);
      }
    
      if (resolvedCachedTokens > 0) {
        lines.push(
          `  Cached input:   ${formatTokenCount(resolvedCachedTokens)} tokens × ${formatCost(model.cache_read_input_token_cost_per_million ?? 0)}/1M = ${formatCost(cachedCost)}`,
        );
      }
    
      if (result.tieredInput) {
        const baseTokens = TIERED_PRICING_THRESHOLD;
        const tieredTokens = uncachedInputTokens - baseTokens;
        lines.push(
          `  Input (base):  ${formatTokenCount(baseTokens)} tokens × ${formatCost(model.input_cost_per_million)}/1M = ${formatCost(result.inputBaseCost)}`,
        );
        lines.push(
          `  Input (>200K): ${formatTokenCount(tieredTokens)} tokens × ${formatCost(model.input_cost_per_million_above_200k ?? 0)}/1M = ${formatCost(result.inputTieredCost)}`,
        );
      } else {
        lines.push(
          `  Input:  ${formatTokenCount(uncachedInputTokens)} tokens × ${formatCost(model.input_cost_per_million)}/1M = ${formatCost(result.inputCost)}`,
        );
      }
    
      if (result.tieredOutput) {
        const baseTokens = TIERED_PRICING_THRESHOLD;
        const tieredTokens = output_tokens - baseTokens;
        lines.push(
          `  Output (base):  ${formatTokenCount(baseTokens)} tokens × ${formatCost(model.output_cost_per_million)}/1M = ${formatCost(result.outputBaseCost)}`,
        );
        lines.push(
          `  Output (>200K): ${formatTokenCount(tieredTokens)} tokens × ${formatCost(model.output_cost_per_million_above_200k ?? 0)}/1M = ${formatCost(result.outputTieredCost)}`,
        );
      } else {
        lines.push(
          `  Output: ${formatTokenCount(output_tokens)} tokens × ${formatCost(model.output_cost_per_million)}/1M = ${formatCost(result.outputCost)}`,
        );
      }
    
      lines.push(`  ─────────────────────────────`);
      lines.push(`  Total:  ${formatCost(totalCost)}`);
    
      return {
        content: [
          {
            type: "text",
            text: lines.join("\n"),
          },
        ],
      };
    }
  • calculateTieredCost helper function that computes input/output costs with optional tiered pricing above the threshold (200K tokens).
    export function calculateTieredCost(
      model: ModelEntry,
      inputTokens: number,
      outputTokens: number,
    ): TieredCostResult {
      const threshold = TIERED_PRICING_THRESHOLD;
    
      let inputBaseCost: number;
      let inputTieredCost = 0;
      let tieredInput = false;
    
      if (model.input_cost_per_token_above_200k != null && inputTokens > threshold) {
        tieredInput = true;
        inputBaseCost = threshold * model.input_cost_per_token;
        inputTieredCost = (inputTokens - threshold) * model.input_cost_per_token_above_200k;
      } else {
        inputBaseCost = inputTokens * model.input_cost_per_token;
      }
    
      let outputBaseCost: number;
      let outputTieredCost = 0;
      let tieredOutput = false;
    
      if (model.output_cost_per_token_above_200k != null && outputTokens > threshold) {
        tieredOutput = true;
        outputBaseCost = threshold * model.output_cost_per_token;
        outputTieredCost = (outputTokens - threshold) * model.output_cost_per_token_above_200k;
      } else {
        outputBaseCost = outputTokens * model.output_cost_per_token;
      }
    
      const inputCost = inputBaseCost + inputTieredCost;
      const outputCost = outputBaseCost + outputTieredCost;
    
      return {
        inputCost,
        outputCost,
        totalCost: inputCost + outputCost,
        tieredInput,
        tieredOutput,
        inputBaseCost,
        inputTieredCost,
        outputBaseCost,
        outputTieredCost,
      };
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided, so description bears full burden. It reveals that cached_tokens must be <= input_tokens and are billed differently, but does not specify behavior for unknown models or whether the tool is idempotent/read-only.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two concise sentences, front-loaded with main purpose. No redundant information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

No output schema and description omits what the tool returns (e.g., estimated cost, currency). Also lacks examples or error cases. Incomplete for a cost estimation tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, baseline 3. Description adds value by clarifying that cached_tokens must be <= input_tokens and that they trigger a different billing rate, which is extra context beyond the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool estimates cost for tokens on a specific model, with mention of cached token discounts. This distinguishes it from sibling tools like compare_models and get_model_details.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance on when to use this tool versus alternatives, nor any when-not-to-use conditions. The description only implies usage for cost estimation.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/atriumn/tokencost-dev'

If you have feedback or need assistance with the MCP directory API, please join our Discord server