How much does an LLM API cost for your app?

An LLM API bills for input and output tokens, priced in dollars per million. In an example with 1,000 messages a day and hypothetical prices, a bot costs $21 to $420 a month, depending on the model.

Updated 7 min read

Learning objectives

After reading this article you will be able to:

  • Understand how per-token billing works and why output costs more
  • Estimate the monthly bill of a bot or app with a step-by-step calculation
  • Cut costs with prompt caching, batch processing and smaller models
On this page

A language model API has no flat monthly fee. The bill follows usage, and usage follows your user count: a bot that starts small and takes off can multiply its spend from one month to the next. That is why you should run the numbers before you launch.

The numbers in this article are hypothetical, chosen to keep the math easy, in US dollars per million tokens. They are not any provider's prices; real rates are on the official pricing pages.

inputoutputTokenseach output tokencosts moreCostSystem promptHistoryQuestionAnswer (output)
Output is the smallest share of tokens but weighs more on the bill: each output token costs more than an input one.

How does an LLM API bill you?#

Billing is per token, a chunk of text that averages about four characters in English. Every call has two counters:

  • Input: everything you send: system prompt, conversation history and the new question.
  • Output: the text the model generates.

Output costs more per token. In the major providers' price lists, output usually costs several times as much as input, and the ratio varies from model to model. That is why long answers weigh more on the bill than long questions.

The cost of one call is:

cost = (input_tokens × input_price + output_tokens × output_price) / 1,000,000

One detail changes the math: the API keeps no state. For the bot to "remember" a conversation, you resend the history on every call, and those tokens are billed again as input. Long conversations get more expensive with every message.

What do models cost?#

Each provider publishes its own price list, and it changes often: see the pricing pages from Anthropic, OpenAI and Google. The math in this article uses round, hypothetical rates per model tier, in USD per million tokens:

Model tier (hypothetical) Input Output Cached input
Small A 0.20 1 0.02
Small B, with pricier output 0.50 4 0.05
Mid-tier 4 20 0.40
Large 10 50 1

The gap between a small and a mid-tier model is usually large: within one provider, the mid-tier model can cost dozens of times what the small one does. And the priciest model is not the right default. For FAQ answers or message classification, the smallest one usually does the job.

Compare cost per task, too, not only price per million. Each model uses its own tokenizer, and the same text can turn into more or fewer tokens depending on the model, even across generations from the same provider. Also watch for promotional pricing: some rates only last until a set date and then go up. Check the end date in the provider's table.

How do you estimate a bot's monthly spend?#

Use three numbers: messages per month, average input tokens and average output tokens. Hypothetical example: a Discord bot that handles 1,000 messages a day:

  • 30,000 calls a month
  • 1,500 input tokens per call (system prompt, short history and question): 45 million a month
  • 400 output tokens per call: 12 million a month

This script runs the math for any model on any recent Node.js version:

js
// cost.mjs: node cost.mjs
const callsPerMonth = 30_000;
const inputTokens = 1_500;
const outputTokens = 400;

// USD per million tokens: hypothetical values, swap in the provider's current prices
const models = {
  "small-a": { in: 0.2, out: 1 },
  "small-b": { in: 0.5, out: 4 },
  "mid-tier": { in: 4, out: 20 },
};

for (const [name, p] of Object.entries(models)) {
  const usd =
    (callsPerMonth * (inputTokens * p.in + outputTokens * p.out)) / 1_000_000;
  console.log(`${name}: $${usd.toFixed(2)} per month`);
}

Result:

Model Cost per month
Small A $21.00
Small B $70.50
Mid-tier $420.00

Changing only the model moves the same workload from $21 to $420. These figures cover the model alone, not hosting the bot.

How do you reduce the API bill?#

In order of payoff, simplest first:

  1. Pick the smallest model that works. Test with 20 real questions before moving up.
  2. Cap the output. Set max_tokens and ask for short answers in the system prompt. Output is the expensive part.
  3. Trim the history. Send only the last 6 to 10 messages, or summarize older ones.
  4. Use prompt caching. If the start of the prompt repeats (long instructions, fixed documents), the provider charges a fraction of the price to read it from cache. A cache read usually costs a small fraction of the input price, and a cache write a bit more than normal input. Some providers also charge for how long content stays in the cache. In the hypothetical mid-tier example, with cache reads at $0.40 per million (the rate from the table), if 1,000 of the 1,500 input tokens are a fixed prefix always read from cache, input drops from $180 to $72 and the total from $420 to $312 (ignoring cache writes, which are rare).
  5. Use batch when there is no rush. Anthropic, OpenAI and Google offer batch APIs that charge less than the standard price in exchange for asynchronous results; the discount is listed on the pricing page. Good for nightly summaries and bulk classification, not for chat.
  6. Set per-user limits. A cooldown of a few seconds per user and a daily message cap keep a single abusive user from burning the budget.

What hidden costs show up?#

  • History resending: a 20-message conversation costs far more than 20 times the first message.
  • Reasoning models: "thinking" tokens count as output. Set a reasoning budget, or turn reasoning off for simple tasks.
  • Long context: some models charge more above a certain prompt size. Check the provider's table to see whether your model has a long-context price tier.
  • Retries: an automatic resend after a network error can bill the same call twice. Cap the retries.
  • Prompt injection: a malicious user can force huge replies. See what prompt injection is and enforce the output cap in code, not only in the prompt.
  • Currency and taxes: the invoice is in dollars. Keep a margin.

How do you set a spending cap?#

Combine three layers. First, the monthly spending limit in the provider's console, which cuts off the API when reached. Second, a counter in your code: add up the tokens the API response reports (usage) and block the user or the server once the daily limit is hit. Finally, your own alert: log the day's spend and notify an admin channel when it passes half the budget.

To build the bot itself, see how to build a Discord AI bot. For the cost of hosting the process, see how much it costs to host a Discord bot. For the underlying concept, read what an LLM is.

How to do this on Square Cloud#

The math above produces two bills: the model provider's, in dollars, and the one for hosting the bot. On Square Cloud you pay for hosting in Brazilian reais, by Pix or card, and from Standard the tokens can come out of the plan itself, within its limits.

The bot runs in an always-on container. A background app uses at least 256 MB of the plan's RAM pool, and Hobby starts at R$12.49 a month with 1 GB. Traffic is not billed by bandwidth or by request, within the fair-use policy. The API key lives in an environment variable on the app, out of your repository, and a change applies on the next restart.

For the token line there is the AI Gateway, available from Standard (R$44.99 a month): an OpenAI-compatible API that serves the cubic model. If your code already uses the OpenAI SDK, switching means the base URL https://api.squarecloud.app/v2/ai, an account API key with the ai:chat scope and the cubic model. It supports function calling in the OpenAI format and searches the web when a question calls for it. It is in beta, with early access at no extra cost, and limits and availability may change when the beta ends.

On the gateway, what caps usage is not a price per million but a daily quota. Standard allows up to 2.5 million tokens and up to 1,000 requests a day, one at a time, with a context window of up to 16k tokens; Pro allows up to 5 million tokens and up to 5,000 requests a day. The example bot, with 1,000 messages and about 1.9 million tokens a day, sits right at Standard's request ceiling; Pro leaves headroom. Keep the max_tokens and per-user limits from this article: they now protect the day's quota instead of your card.

See the code samples on AI Gateway and compare plans on pricing.

AI

Host AI apps in always-on containers

Learn more

FAQs

How much do 1,000 messages a day cost on an LLM API?

At 1,500 input tokens and 400 output tokens per message, that is 30,000 calls a month. With the article's hypothetical prices, it comes to $21 on a small model and $420 on a mid-tier one. Real rates are on the provider's page.

Why do output tokens cost more than input tokens?

In the major providers' price lists, output usually costs several times the input price. The model generates its reply one token at a time, which costs more than reading the whole prompt at once.

Is there a no-cost LLM API for testing?

Some providers offer test usage at no charge with low limits. Each provider's pricing page shows which models that allowance covers. A bot with real users usually outgrows those limits and needs the paid tier.

Does prompt caching really lower the bill?

Yes, when the start of the prompt repeats. Reading a cached prefix usually costs a small fraction of the normal input price. It pays off with long system prompts.

Can I cap the spend of a public bot?

Yes. Limit reply length with max_tokens, add a per-user cooldown, trim the history you send, and set a spending limit in the provider's console.

Next articleHow to host n8nHosting n8n means running its server on an always-on process with a public HTTPS URL, a database and a fixed encryption key. Here is the step by step.