Aerostack
memory

ai-cost-estimate Edge Function — Ai

AI

Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function ai-cost-estimate Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/ai-cost-estimate
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "ai-cost-estimate",
  args: {
    "model": "example_model",
    "promptTokens": 42,
    "completionTokens": 0,
    "currency": "USD"
  }
})

ai-cost-estimate — Calculate LLM API request cost

Computes the cost of an LLM API call given a model, prompt token count, and completion token count — supports USD, EUR, and GBP with no external API calls.


Supported Models

Model Prompt (per 1M) Completion (per 1M)
gpt-4o $2.50 $10.00
gpt-4o-mini $0.15 $0.60
gpt-4-turbo $10.00 $30.00
gpt-3.5-turbo $0.50 $1.50
claude-3-5-sonnet $3.00 $15.00
claude-3-5-haiku $0.80 $4.00
claude-3-opus $15.00 $75.00
claude-sonnet-4-6 $3.00 $15.00
gemini-1.5-pro $1.25 $5.00
gemini-1.5-flash $0.075 $0.30

API

POST /api/ai-cost-estimate

Request body

Field Type Required Default Description
model string Model identifier
promptTokens number Number of prompt/input tokens
completionTokens number 0 Number of completion/output tokens
currency "USD" | "EUR" | "GBP" "USD" Output currency

Success response (200)

{
  "success": true,
  "data": {
    "promptCost": 0.0025,
    "completionCost": 0.01,
    "totalCost": 0.0125,
    "currency": "USD",
    "model": "gpt-4o",
    "pricePerMillion": { "prompt": 2.5, "completion": 10.0 }
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing or invalid field
INTERNAL_ERROR 500 Unknown model

Usage

cURL
curl -X POST "$FUNCTION_URL" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-4o", "promptTokens": 1500, "completionTokens": 800, "currency": "EUR"}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ model: 'claude-3-5-sonnet', promptTokens: 2000, completionTokens: 500 }),
});
const { data } = await response.json();
console.log(`Total cost: $${data.totalCost}`);
Direct import (Node / Bun / Deno)
import { aiCostEstimate } from '@aerostack/functions/ai-cost-estimate';

const { totalCost } = aiCostEstimate({ model: 'gpt-4o-mini', promptTokens: 5000, completionTokens: 1000 });

Use Cases

  • Displaying real-time cost estimates to users in an AI product before they submit a request.
  • Tracking per-request LLM spend in an application analytics dashboard.
  • Comparing cost across models to choose the most cost-effective option for a given task.

Notes

  • Prices are approximate as of early 2026 and may change — update MODEL_PRICING in core.ts as needed.
  • EUR rate: 0.92, GBP rate: 0.79 relative to USD.
  • Unknown models throw a clear error listing all supported model names.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

ai
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More AI Functions

Browse AI Functions →

Frequently asked questions

What does the ai-cost-estimate function do? +

ai-cost-estimate is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the ai-cost-estimate function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-cost-estimate ``` It will be live on Cloudflare Workers in seconds.

What runtime does ai-cost-estimate use? +

ai-cost-estimate runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the ai-cost-estimate function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.