# LLM Token Counter & Cost Estimator (Claude/GPT/Gemini/Llama) (`gochujang/llm-token-counter`) Actor

Count tokens for any text across 16+ models (Claude Opus/Sonnet/Haiku, GPT-4o, o3, Gemini 1.5, Llama, Mistral) and estimate per-million-token cost. Claude via Anthropic API (BYO key), GPT via tiktoken, others via heuristic. $0.001 per text counted.

- **URL**: https://apify.com/gochujang/llm-token-counter.md
- **Developed by:** [Hojun Lee](https://apify.com/gochujang) (community)
- **Categories:** Developer tools, Automation, Other
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage, which gets cheaper the higher subscription plan you have.

Learn more: https://docs.apify.com/platform/actors/running/actors-in-store#pay-per-usage

## What's an Apify Actor?

Actors are a software tools running on the Apify platform, for all kinds of web data extraction and automation use cases.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

In JavaScript/TypeScript projects, use official [JavaScript/TypeScript client](https://docs.apify.com/api/client/js.md):

```bash
npm install apify-client
```

In Python projects, use official [Python client library](https://docs.apify.com/api/client/python.md):

```bash
pip install apify-client
```

In shell scripts, use [Apify CLI](https://docs.apify.com/cli/docs.md):

````bash
# MacOS / Linux
curl -fsSL https://apify.com/install-cli.sh | bash
# Windows
irm https://apify.com/install-cli.ps1 | iex
```bash

In AI frameworks, you might use the [Apify MCP server](https://docs.apify.com/platform/integrations/mcp.md).

If your project is in a different language, use the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).


# README

## LLM Token Counter & Cost Estimator

> Count tokens for any text across **16+ models** (Claude Opus 4.7, Sonnet 4.6, Haiku 4.5, GPT-4o, o3, Gemini 1.5, Llama 3, Mistral) and estimate **per-call cost in USD**. Claude via Anthropic SDK (BYO key, free endpoint), GPT via tiktoken (no key needed), others via heuristic. **$0.001 per text counted.**

---

### Why this exists

Every LLM engineer asks the same question 50 times a day: "if I send this prompt, how many tokens — and how much will it cost?"

Existing solutions:
- **OpenAI tokenizer tool**: web-only, OpenAI models only
- **Anthropic count_tokens API**: free but requires writing the SDK call
- **Chargify / LLM-monitor SaaS**: $50+/mo

This actor wraps all the major tokenizers and prices in one call. Drop a prompt in, get a sorted cost comparison across every model that could handle it.

---

### What you get

#### Summary row
```json
{
  "_type": "summary",
  "char_count": 5230,
  "models_compared": 16,
  "cheapest_model": "gemini-1.5-flash",
  "cheapest_total_usd": 0.000405,
  "most_expensive_model": "claude-opus-4-5"
}
````

#### Per-model row (one per model)

```json
{
  "_type": "model_count",
  "model": "claude-opus-4-7",
  "input_tokens": 1320,
  "chars": 5230,
  "method": "anthropic_count_tokens",
  "input_price_per_1m": 5.00,
  "output_price_per_1m": 25.00,
  "max_output_tokens": 1024,
  "input_cost_usd": 0.0066,
  "max_output_cost_usd": 0.0256,
  "total_cost_usd": 0.0322
}
```

Rows are sorted by **input cost ascending** — so the cheapest option is the first model row.

***

### Supported models

| Model | Tokenizer | Pricing source |
|---|---|---|
| `claude-opus-4-7` | Anthropic count\_tokens (or cl100k proxy) | $5 / $25 per 1M |
| `claude-opus-4-6` | same | $5 / $25 |
| `claude-sonnet-4-6` | same | $3 / $15 |
| `claude-haiku-4-5` | same | $1 / $5 |
| `gpt-4o` | tiktoken o200k\_base | $2.50 / $10 |
| `gpt-4o-mini` | same | $0.15 / $0.60 |
| `gpt-4-turbo` | cl100k\_base | $10 / $30 |
| `gpt-3.5-turbo` | cl100k\_base | $0.50 / $1.50 |
| `o3` | o200k\_base | $15 / $60 |
| `gemini-1.5-pro` | heuristic ~4 chars/token | $1.25 / $5 |
| `gemini-1.5-flash` | heuristic | $0.075 / $0.30 |
| `llama-3-70b` | heuristic | $0.59 / $0.79 |
| `mistral-large` | heuristic | $2 / $6 |

***

### Quick start

#### Single text

```json
{
  "text": "Hello, world. Today I am going to..."
}
```

#### From URL

```json
{
  "textUrl": "https://en.wikipedia.org/wiki/Bitcoin"
}
```

#### Exact Claude counts (with Anthropic key — recommended)

```json
{
  "text": "...",
  "anthropicApiKey": "sk-ant-..."
}
```

#### Compare only models you care about

```json
{
  "text": "...",
  "models": ["claude-opus-4-7", "claude-sonnet-4-6", "gpt-4o", "gemini-1.5-pro"]
}
```

#### Realistic cost (set expected output length)

```json
{
  "text": "...",
  "maxOutputTokens": 4000
}
```

***

### Use cases

1. **Cost-sensitive prompt design** — Try Opus 4.7 vs Sonnet 4.6 vs Haiku for the same prompt; pick cheapest that still works
2. **RAG chunk sizing** — Check if your retrieval chunks + system prompt fit in the context window
3. **Batch budget forecasting** — Run on 100 sample inputs → multiply for full corpus
4. **Provider comparison** — At what input length does Claude become cheaper than GPT? This actor answers in one call
5. **Education** — Show students why "translate this novel" hits the wall fast

***

### Pricing

**Pay-Per-Event**: `$0.001 per text counted` (regardless of how many models).

Vs Helicone ($25-100/mo for cost tracking), LangSmith, or building your own.

***

### How counting works per provider

- **OpenAI (gpt-4o family)** — exact via `tiktoken` library
- **Claude** — exact via Anthropic SDK `count_tokens` (free endpoint, requires `anthropicApiKey`). Without the key, falls back to `cl100k` tokenizer as a proxy (typically within ±5%).
- **Gemini / Llama / Mistral** — heuristic: ~4 chars per token. Accuracy varies by language. For Korean / Japanese / Chinese this overestimates by ~20%.

***

### Related actors (same author)

- [Article Summarizer](https://apify.com/gochujang/article-summarizer) — uses Claude
- [Web Structured Extractor](https://apify.com/gochujang/web-structured-extractor)
- [PDF → RAG Chunks](https://apify.com/gochujang/pdf-rag-chunker)
- [JSON Schema Generator](https://apify.com/gochujang/json-schema-generator)

***

### Feedback

A short review helps LLM engineers find it: [Leave a review on Apify Store](https://apify.com/gochujang/llm-token-counter#reviews)

# Actor input Schema

## `text` (type: `string`):

Raw text. Used if 'textUrl' is empty.

## `textUrl` (type: `string`):

Used if 'text' is empty. Returns raw HTTP body.

## `models` (type: `array`):

Which models to count for. Empty = all 16 known models.

## `maxOutputTokens` (type: `integer`):

Assumed output length for cost calculation. Set to your typical generation budget.

## `anthropicApiKey` (type: `string`):

Get one at console.anthropic.com. Without it, Claude counts fall back to a tiktoken proxy (close-ish). Anthropic count\_tokens endpoint is free.

## `userAgent` (type: `string`):

Custom UA when fetching textUrl.

## Actor input object example

```json
{
  "text": "",
  "textUrl": "",
  "models": [
    "claude-opus-4-7",
    "claude-sonnet-4-6",
    "claude-haiku-4-5",
    "gpt-4o",
    "gpt-4o-mini",
    "gemini-1.5-pro",
    "gemini-1.5-flash"
  ],
  "maxOutputTokens": 1024,
  "userAgent": ""
}
```

# Actor output Schema

## `dataset` (type: `string`):

No description

## `summary` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "models": [
        "claude-opus-4-7",
        "claude-sonnet-4-6",
        "claude-haiku-4-5",
        "gpt-4o",
        "gpt-4o-mini",
        "gemini-1.5-pro",
        "gemini-1.5-flash"
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("gochujang/llm-token-counter").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "models": [
        "claude-opus-4-7",
        "claude-sonnet-4-6",
        "claude-haiku-4-5",
        "gpt-4o",
        "gpt-4o-mini",
        "gemini-1.5-pro",
        "gemini-1.5-flash",
    ] }

# Run the Actor and wait for it to finish
run = client.actor("gochujang/llm-token-counter").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "models": [
    "claude-opus-4-7",
    "claude-sonnet-4-6",
    "claude-haiku-4-5",
    "gpt-4o",
    "gpt-4o-mini",
    "gemini-1.5-pro",
    "gemini-1.5-flash"
  ]
}' |
apify call gochujang/llm-token-counter --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "command": "npx",
            "args": [
                "mcp-remote",
                "https://mcp.apify.com/?tools=gochujang/llm-token-counter",
                "--header",
                "Authorization: Bearer <YOUR_API_TOKEN>"
            ]
        }
    }
}

```

## OpenAPI specification

```json
{
    "openapi": "3.0.1",
    "info": {
        "title": "LLM Token Counter & Cost Estimator (Claude/GPT/Gemini/Llama)",
        "description": "Count tokens for any text across 16+ models (Claude Opus/Sonnet/Haiku, GPT-4o, o3, Gemini 1.5, Llama, Mistral) and estimate per-million-token cost. Claude via Anthropic API (BYO key), GPT via tiktoken, others via heuristic. $0.001 per text counted.",
        "version": "0.1",
        "x-build-id": "UV2ERXMLJgyCssguq"
    },
    "servers": [
        {
            "url": "https://api.apify.com/v2"
        }
    ],
    "paths": {
        "/acts/gochujang~llm-token-counter/run-sync-get-dataset-items": {
            "post": {
                "operationId": "run-sync-get-dataset-items-gochujang-llm-token-counter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for its completion, and returns Actor's dataset items in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        },
        "/acts/gochujang~llm-token-counter/runs": {
            "post": {
                "operationId": "runs-sync-gochujang-llm-token-counter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor and returns information about the initiated run in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/runsResponseSchema"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/acts/gochujang~llm-token-counter/run-sync": {
            "post": {
                "operationId": "run-sync-gochujang-llm-token-counter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for completion, and returns the OUTPUT from Key-value store in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "inputSchema": {
                "type": "object",
                "properties": {
                    "text": {
                        "title": "Text to count",
                        "type": "string",
                        "description": "Raw text. Used if 'textUrl' is empty.",
                        "default": ""
                    },
                    "textUrl": {
                        "title": "Or: URL to fetch text from",
                        "type": "string",
                        "description": "Used if 'text' is empty. Returns raw HTTP body.",
                        "default": ""
                    },
                    "models": {
                        "title": "Models",
                        "type": "array",
                        "description": "Which models to count for. Empty = all 16 known models.",
                        "default": [],
                        "items": {
                            "type": "string"
                        }
                    },
                    "maxOutputTokens": {
                        "title": "Max output tokens (for cost estimate)",
                        "minimum": 1,
                        "maximum": 1000000,
                        "type": "integer",
                        "description": "Assumed output length for cost calculation. Set to your typical generation budget.",
                        "default": 1024
                    },
                    "anthropicApiKey": {
                        "title": "Anthropic API key (optional, exact Claude counts)",
                        "type": "string",
                        "description": "Get one at console.anthropic.com. Without it, Claude counts fall back to a tiktoken proxy (close-ish). Anthropic count_tokens endpoint is free."
                    },
                    "userAgent": {
                        "title": "User-Agent (for textUrl)",
                        "type": "string",
                        "description": "Custom UA when fetching textUrl.",
                        "default": ""
                    }
                }
            },
            "runsResponseSchema": {
                "type": "object",
                "properties": {
                    "data": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "actId": {
                                "type": "string"
                            },
                            "userId": {
                                "type": "string"
                            },
                            "startedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "finishedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "status": {
                                "type": "string",
                                "example": "READY"
                            },
                            "meta": {
                                "type": "object",
                                "properties": {
                                    "origin": {
                                        "type": "string",
                                        "example": "API"
                                    },
                                    "userAgent": {
                                        "type": "string"
                                    }
                                }
                            },
                            "stats": {
                                "type": "object",
                                "properties": {
                                    "inputBodyLen": {
                                        "type": "integer",
                                        "example": 2000
                                    },
                                    "rebootCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "restartCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "resurrectCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "computeUnits": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "options": {
                                "type": "object",
                                "properties": {
                                    "build": {
                                        "type": "string",
                                        "example": "latest"
                                    },
                                    "timeoutSecs": {
                                        "type": "integer",
                                        "example": 300
                                    },
                                    "memoryMbytes": {
                                        "type": "integer",
                                        "example": 1024
                                    },
                                    "diskMbytes": {
                                        "type": "integer",
                                        "example": 2048
                                    }
                                }
                            },
                            "buildId": {
                                "type": "string"
                            },
                            "defaultKeyValueStoreId": {
                                "type": "string"
                            },
                            "defaultDatasetId": {
                                "type": "string"
                            },
                            "defaultRequestQueueId": {
                                "type": "string"
                            },
                            "buildNumber": {
                                "type": "string",
                                "example": "1.0.0"
                            },
                            "containerUrl": {
                                "type": "string"
                            },
                            "usage": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "integer",
                                        "example": 1
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "usageTotalUsd": {
                                "type": "number",
                                "example": 0.00005
                            },
                            "usageUsd": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "number",
                                        "example": 0.00005
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
```
