Jump straight to the numbers

← All articles

A $5 Telegram AI Bot With Pay-Per-Token Billing

01 / Set a hard $5 limit before writing code

A Telegram AI bot does not need a server with a large monthly bill. For a beginner project, set aside $5 as a testing budget and treat it as a spending cap. Your actual cost depends on the model, input tokens, output tokens, and any cache reads or writes. Do not assume that five dollars buys a fixed number of messages.

CheapRelay is a pay-as-you-go AI API relay with no monthly fee and no subscription. Billing is based on real token usage, including cache reads and writes. One API key can reach multiple providers, so you can change models without rebuilding the Telegram side of the bot.

Sign up at CheapRelay, create an API key, and add only the amount you are prepared to spend. Keep the key private. Never paste it into a public Git repository or put it directly in code that you plan to share.

02 / Create the Telegram bot and store two secrets

Open Telegram and message BotFather. Create a new bot, choose a username, and copy the bot token it gives you. The token lets software control the bot, so handle it like a password.

You now need two environment variables: TELEGRAM_BOT_TOKEN for Telegram and CHEAPRELAY_API_KEY for CheapRelay. On a local computer, place them in your shell environment or in a local .env file that is excluded from version control. Keeping credentials outside the source code makes replacement easier if one is exposed.

03 / Use the OpenAI-compatible route from Python

Install Python and the Telegram bot package used by your chosen framework. You also need the OpenAI SDK, because CheapRelay provides OpenAI-compatible routes under https://api.cheapapirelay.com/v1. Set the SDK base URL to https://api.cheapapirelay.com/v1 and authenticate with your CheapRelay key.

The basic request has three parts: a model name, a short system instruction, and the user's Telegram message. For a low-cost first test, choose a small model such as Gemini 2.5 Flash. Set a conservative maximum output token value so a single unusual request cannot generate an unexpectedly long answer.

A minimal request conceptually looks like this: create an OpenAI client with the CheapRelay base URL, send the incoming text as a user message, select the model, and read the returned text. The Telegram handler then sends that text back to the same chat. The exact handler code depends on whether you use polling or webhooks, but the API call remains the same.

04 / Keep the first bot deliberately narrow

Start with one command and one behavior: the user sends a message, the bot asks the model, and the bot replies. Add a /start response explaining that the bot is experimental. Add an /about command showing the selected model and a reminder that usage is metered.

  • ■ Reject empty messages.
  • ■ Limit incoming message length before sending it to the model.
  • ■ Set a maximum output token count.
  • ■ Return a short error when the API is unavailable.
  • ■ Log request time and token usage without logging private message content.

These controls improve cost predictability and make debugging easier. They do not guarantee a fixed bill, because token counts vary with every conversation.

05 / Track tokens instead of guessing from message count

Ten short messages can cost less than one long pasted document. A conversation that sends previous messages on every request also grows more expensive over time. For the first version, do not retain unlimited chat history. Keep only a small number of recent turns, or send each message independently.

Check the usage information returned by the API and review your CheapRelay account regularly. Separate testing into small sessions, such as one dollar at a time. When the remaining balance is low, make the bot return a clear budget message instead of continuing to send requests.

06 / Move from local testing to a small deployment

Polling is suitable for initial testing because the bot connects outward to Telegram. Once the behavior is stable, run it on a small always-on host if you need continuous availability. Store both secrets in the host's environment settings, restart the process after configuration changes, and add a simple process monitor.

Before sharing the bot, test long messages, empty messages, API errors, repeated requests, and a revoked API key. The useful beginner milestone is not a feature-rich assistant. It is a Telegram bot that responds reliably, exposes no secrets, and makes every paid request visible enough to control.

Sign up Try the prices yourself, sign-up credit included.

More reading

DateAisleWritten inTitle
2026-10-28Spend lessEN$0.0012 for a Tool Loop—With Assumed Rates
A beginner’s guide to function calling, tool schemas, repeated context, and keeping a two-request AI workflow within a tiny budget.
2026-10-28Spend lessEN$2.78 for 10,000 Documents: A Lean API Budget
A beginner walkthrough for processing 10,000 documents cheaply, with transparent token math, local extraction, a small pilot, and retry controls.
2026-10-28Spend lessEN$0.003 per request: count tokens before sending
Estimate input tokens, cap output, and check actual usage with free tools and simple math before spending a tiny API budget.
2026-10-28Spend lessEN$1 for 1,000 Ticket Summaries: Check the Token Math
A beginner’s $1 planning budget for support-ticket summaries: estimate tokens, test a small model, and control retries before processing the queue.
2026-10-28Spend lessEN8 Million Fewer Tokens From One Prompt Trim
Trim a repeated system prompt on a tiny budget, calculate the savings, and check that shorter instructions still produce useful answers.
2026-10-28Spend lessENA $5 AI Budget Starts With a $0.96 Estimate
Estimate a small monthly AI bill with token math, repeat calls, and a spending buffer before you write any code.