ai-cost-estimate Edge Function — Ai
AICalculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
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.
npx aerostack add navin/ai-cost-estimate Use with AI Assistants
MCPConnect 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.
{
"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({
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_PRICINGincore.tsas needed. - EUR rate: 0.92, GBP rate: 0.79 relative to USD.
- Unknown models throw a clear error listing all supported model names.
Metadata
Tags
Publisher
@navin verified
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 →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-context-window-fit
by @navin
Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-language-detect
by @navin
Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
ai-messages-to-prompt
by @navin
Serialises a structured message array into a formatted prompt string for open-source LLMs, supporting ChatML, Llama 2, Alpaca, and plain text formats.
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.