Running a Telegram bot on GPT-4 is like hiring a lawyer to answer your phone. It works, but you are hemorrhaging money on tasks that do not need that level of capability. Most Telegram bot messages are casual: "remind me to call the dentist," "what is 15% of 340," "translate this sentence to French." These are trivial tasks for any LLM, but if your bot is hardcoded to GPT-4o, every single one costs premium tokens.
Antbase turns your Telegram bot into a cost-efficient powerhouse. Each incoming message gets classified based on its actual complexity. "What time is it in Tokyo" goes to a fast free model and responds in under a second. "Explain the trade-offs between microservices and monoliths for a team of five" gets routed to a premium model that can give a thoughtful answer. Your users get the same quality experience — they never know which model handled their request — but your costs drop by 70-80% because most conversations are simple.
The reliability angle matters too. Telegram bots are always-on services. If your hardcoded OpenAI endpoint goes down, your bot goes silent. With Antbase, there is automatic failover across providers. Your bot stays responsive 24/7, even during provider outages. For a personal assistant that people rely on daily, that resilience is essential.
A Telegram bot backed by Antbase gives you a personal AI assistant in your pocket. Messages get routed to the best model — quick questions go to fast models, complex requests go to premium ones. This guide uses python-telegram-bot and the OpenAI SDK.
Setup
pip install python-telegram-bot openai
# Get a bot token from @BotFather on Telegram
# Set environment variables
export TELEGRAM_BOT_TOKEN="your-telegram-token"
export ANTBASE_API_KEY="ant_your-api-key"Bot Code
import os
from openai import OpenAI
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
client = OpenAI(
base_url="https://antbase.ai/v1",
api_key=os.environ["ANTBASE_API_KEY"],
)
# Store conversation history per chat
conversations: dict[int, list] = {}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
conversations[update.effective_chat.id] = []
await update.message.reply_text("Hi! I'm an AI assistant. Send me a message.")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
user_msg = update.message.text
if chat_id not in conversations:
conversations[chat_id] = []
conversations[chat_id].append({"role": "user", "content": user_msg})
# Keep last 20 messages for context
messages = conversations[chat_id][-20:]
response = client.chat.completions.create(
model="auto",
messages=[
{"role": "system", "content": "You are a helpful Telegram assistant. Keep replies concise."},
*messages,
],
)
reply = response.choices[0].message.content
conversations[chat_id].append({"role": "assistant", "content": reply})
await update.message.reply_text(reply)
app = ApplicationBuilder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
app.run_polling()Tips
- •Telegram has a 4096-character message limit. For long responses, split the reply into chunks before sending.
- •Add a /clear command to reset conversation history. Long histories increase token usage and cost.
- •For production, use webhooks instead of polling and persist conversations in a database (Redis or SQLite).
- •Antbase routes short conversational messages to fast models, so typical chat feels snappy. Complex requests like code generation may take a moment as they route to premium models.



