AI Telegram bot setup via the Kiree gateway
A ready bot.py for a Telegram bot with an LLM: aiogram 3 or python-telegram-bot, dialog history, 4096-character reply splitting, base_url https://kiree.net/v1, billed by tokens.
Why the direct setup does not cover every case
The bot normally calls the vendor endpoint. Set the OpenAI-compatible base_url to the gateway, keep a 20-message history per chat, and reply with catalog models through kiree.net.
Steps to your first request
Two tokens
The bot token from @BotFather and a sk-… key in the kiree.net console.
Install dependencies
pip install openai aiogram — or openai python-telegram-bot for the second variant below.
Copy bot.py
Ready example: text handler, 20-message history, typing indicator and 4096-character reply splitting.
Run and check
TG_TOKEN=… KIREE_KEY=sk-… python bot.py; message the bot: the reply comes from a gateway model.
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from openai import AsyncOpenAI
bot = Bot(os.environ["TG_TOKEN"])
dp = Dispatcher()
ai = AsyncOpenAI(base_url="https://kiree.net/v1", api_key=os.environ["KIREE_KEY"])
from telegram.ext import Application, MessageHandler, filters, ContextTypes
from openai import OpenAI
ai = OpenAI(base_url="https://kiree.net/v1", api_key="sk-…")
Take keys from the TG_TOKEN and KIREE_KEY variables, not from code. Sending without parse_mode avoids Markdown formatting errors in model replies; history is trimmed to 20 messages so input tokens do not grow without limit.
Add dialog history the same way as the aiogram example: a messages list in ctx.chat_data trimmed to the last 20.
Check: TG_TOKEN=… KIREE_KEY=sk-… python bot.py and any message to the bot: the reply comes from a gateway model, token usage visible in the kiree.net console.
What to connect in AI Telegram Bot
Any text model from the catalog works on one key. Below are popular options to start.
Common errors
401 Unauthorized
Wrong sk-… key or base_url without /v1: check both lines.
429 / insufficient_quota
Balance exhausted: top up the kiree.net balance; keep a buffer for a bot with steady traffic.
Telegram Bad Request: message too long
The model reply exceeds 4096 characters: split it with the split function in the example.
Bad Request: can't parse entities
The model returned Markdown (*, _, [) and the bot sends with parse_mode: send plain text without parse_mode or escape the markup.
Conflict: terminated by other getUpdates
Two polling processes are running with the same bot token: stop the extra instance.
ReadTimeout / APITimeoutError
A long generation did not fit the SDK timeout: raise the timeout (120 s in the example) and cap the reply max_tokens.
Frequently asked questions
How does the AI Telegram bot run through the gateway?+
Model requests go to kiree.net directly; the Telegram API itself is reachable normally, and the bot can be hosted on your own VPS.
What does 1000 bot dialogs cost?+
On gpt-6-luna about $0.20 without history (300 input + 500 output tokens per dialog); with a 20-message history input grows 2–3×. The estimate table is in the Economics section.
Which model is cheapest for a bot?+
gpt-6-luna is the most budget option for streaming dialogs; gpt-4o-mini and claude-haiku-5-5 are mid-tier; flagship models are for complex tasks.
How does the bot remember dialog context?+
Keep a messages list per chat.id (history in the example) and pass it in messages; trim to the last 20 to control input tokens.
Do I need an OpenAI account?+
No: the kiree.net key replaces it; usage is billed by tokens from the balance.
Can Claude be connected to the bot?+
Yes: the same OpenAI-compatible endpoint accepts claude-* models from the catalog; only the model string changes.
Polling or webhook?+
For a start and one instance, polling (the example above). Webhook makes sense with heavy traffic and several instances behind a balancer.
Create a key in a couple of minutes; pay only for actual usage, no subscription required.