Bad Request: query is too old and response timeout expired or query ID is invalid — how to fix it
Bad Request: query is too old and response timeout expired or query ID is invalid means answerCallbackQuery came too late for a button press, or got the wrong ID. The handler did slow work before answering, or the bot was down and is now working through presses that queued up. Answer the callback query first, do the slow work after, and ignore this error for stale presses.
By Livops team, the people who build and run LivopsUpdated
The error, as the Telegram Bot API returns it for answerCallbackQuery:
{"ok":false,"error_code":400,"description":"Bad Request: query is too old and response timeout expired or query ID is invalid"}
Each library wraps it in its own words:
| Library | What you see |
|---|---|
| aiogram 3 | TelegramBadRequest: Telegram server says - Bad Request: query is too old and response timeout expired or query ID is invalid[4][5] |
| python-telegram-bot | telegram.error.BadRequest: Query is too old and response timeout expired or query id is invalid[10][11] |
| grammY | GrammyError: Call to 'answerCallbackQuery' failed! (400: Bad Request: query is too old ...)[14] |
| Telegraf | TelegramError: 400: Bad Request: query is too old and response timeout expired or query ID is invalid[17] |
This page explains what the error means, lists the causes from the most common, and shows the fixes — answer first, deal with the backlog after a restart, ignore stale presses — in aiogram, python-telegram-bot, grammY and Telegraf. It ends with how it looks on Livops.
What does the error mean?
When a user presses an inline button with callback data, the bot receives a CallbackQuery. Telegram clients show a progress bar until the bot calls answerCallbackQuery, so the bot must call it even when it has nothing to show the user[1]. The answer can be a notification at the top of the chat or an alert; its only required parameter is callback_query_id, the unique identifier of the query[1].
Telegram’s servers report a refused answer as QUERY_ID_INVALID, and the Bot API server turns it into the text above with code 400 and the prefix Bad Request[2]. As the text says, there are two possibilities: the answer came after the query timed out, or the ID is not a valid query ID.
How long is too long? The Bot API page does not give a number for answerCallbackQuery[1]. grammY’s documentation notes that some clients may show a loading animation for up to a minute when a bot does not react to a press[3]; that is how long the user may wait, not a deadline you can plan around. Treat it simply: answer as soon as the update arrives.
Match the error by its description, not only by its code: a failed request has ok set to false and the reason in description, and the contents of error_code are subject to change[1].
Causes, most common first
1. Slow work before the answer
The handler calls a database, an external API or a language model, builds a file or sends a broadcast, and calls answerCallbackQuery only at the end. By then the press is too old.
A middleware can hide this. aiogram’s CallbackAnswerMiddleware answers every handled callback query automatically, and by default it does that after the handler has finished[6][7], so a slow handler means a late answer even though you never wrote callback.answer() at the end yourself.
2. The bot was down and is working through a backlog
Updates wait on Telegram’s server until the bot receives them, for up to 24 hours[1]. Buttons pressed while the bot was stopped, crashing or restarting — during a deploy, for example — arrive together when it starts again. They are already too old to answer, so each one fails in a row in the log, and then the errors stop.
3. Updates handled one at a time
If one slow handler holds the queue, every press behind it is answered late too. grammY’s built-in bot.start() handles a batch of updates one by one, waiting for each[15]. aiogram’s start_polling runs each update as its own task by default (handle_as_tasks=True)[9], so one slow handler does not hold up the others there.
4. The wrong ID, or an answer the query does not expect
callback_query_id must be the id of the CallbackQuery the bot received[1]. A message ID, the callback data, a chat ID or an ID saved from an earlier press does not work. If the error comes even on fast, fresh presses, check what you pass — and check that one press is answered once: with aiogram’s middleware on, change the answer through the callback_answer argument, or disable it for that handler[6], rather than also calling callback.answer() yourself.
How to fix it
Answer first, then do the slow work
Call answerCallbackQuery at the top of the handler — with a short “Working on it…” text if the work takes a while — and send the result afterwards as a new message or an edit.
# aiogram 3
@router.callback_query(F.data == "report")
async def report(callback: CallbackQuery):
await callback.answer("Preparing the report…")
text = await build_report() # slow
await callback.message.answer(text)
# python-telegram-bot
async def report(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer("Preparing the report…")
text = await build_report()
await query.message.reply_text(text)
// grammY
bot.callbackQuery("report", async (ctx) => {
await ctx.answerCallbackQuery("Preparing the report…");
const text = await buildReport();
await ctx.reply(text);
});
// Telegraf
bot.action("report", async (ctx) => {
await ctx.answerCbQuery("Preparing the report…");
const text = await buildReport();
await ctx.reply(text);
});
query.answer(), ctx.answerCallbackQuery() and ctx.answerCbQuery() are each library’s shortcut for answering the current query[12][16][19]. In aiogram with CallbackAnswerMiddleware, pass pre=True to answer before the handler runs; then the answer’s text can no longer be changed inside the handler[6][7].
For work that takes minutes, answer, then hand the job to a queue or a background worker so the handler returns at once.
After a restart: drop the backlog, or ignore it
Both setWebhook and deleteWebhook take drop_pending_updates: pass true to drop all pending updates[1]. Each library exposes it at start:
| Library | On start |
|---|---|
| aiogram 3 | await bot.delete_webhook(drop_pending_updates=True) before start_polling[8] |
| python-telegram-bot | application.run_polling(drop_pending_updates=True)[13] |
| grammY | bot.start({ drop_pending_updates: true })[15] |
| Telegraf | bot.launch({ dropPendingUpdates: true })[18] |
This drops every pending update — messages and commands too — not only old presses. Use it where a missed message does not matter; otherwise keep the backlog and ignore this one error for the stale presses.
Catch this exact error and ignore it
A stale press has no one left waiting for its answer. Catch the error around the answer, check its text, and raise everything else.
# aiogram 3: TelegramBadRequest carries Telegram's description
from aiogram.exceptions import TelegramBadRequest
try:
await callback.answer()
except TelegramBadRequest as e:
if "query is too old" not in e.message:
raise
# python-telegram-bot: the text starts with "Query is too old", so compare in lower case
from telegram.error import BadRequest
try:
await query.answer()
except BadRequest as e:
if "query is too old" not in e.message.lower():
raise
// grammY
import { GrammyError } from "grammy";
await ctx.answerCallbackQuery().catch((e) => {
if (!(e instanceof GrammyError && e.description.includes("query is too old"))) throw e;
});
// Telegraf
import { TelegramError } from "telegraf";
await ctx.answerCbQuery().catch((e) => {
if (!(e instanceof TelegramError && e.description.includes("query is too old"))) throw e;
});
The error classes are aiogram’s TelegramBadRequest[4][5], python-telegram-bot’s BadRequest, which drops the Bad Request: prefix and capitalizes the rest[10][11], grammY’s GrammyError with description[14], and Telegraf’s TelegramError with description[17].
Catching matters most in grammY and Telegraf. grammY with no bot.catch stops the bot on an uncaught error while bot.start() runs[15], and Telegraf’s default handler rethrows it[18] — so a backlog of old presses after a restart can take the bot down again.
On Livops
- A bot is down during a deploy. A bot’s old version is stopped before the new one starts, because two copies polling Telegram would conflict, so the bot is down until a deploy succeeds. Presses made in that gap wait on Telegram and reach the new version when it starts — cause 2 above, briefly, after a deploy. See The app did not start.
- If the backlog stops the bot. If an uncaught error stops the bot right after it starts, the deploy fails as
start_failed, and the bot’s last words are on the Logs page. - Logs. The error lines are on the project’s Logs page;
livops logs <service> -ffollows them from the terminal (CLI), andget_logsover MCP returns up to 5000 lines. A short burst right after a deploy is the backlog; errors on fresh presses mean a slow answer. - Polling or webhook. Both work. With a webhook, Livops gives Telegram the address itself, so do not call
setWebhookfrom your code — nordeleteWebhook, which removes the webhook[1]. The start options in the table above are for a bot that polls.
A bot costs 19 credits a month (19,000 so’m); see Pricing.
Questions
What does query is too old and response timeout expired mean?
Your bot called answerCallbackQuery for a button press that Telegram no longer accepts an answer for, or with an ID that is not a valid callback query ID. Most often the answer simply came too late: the handler did slow work first, or the press waited in the queue while the bot was down.
How long does a bot have to answer a callback query?
The Bot API documentation does not give a number for answerCallbackQuery. It says Telegram clients show a progress bar until the bot calls answerCallbackQuery, and that the bot should call it even when it has nothing to show. Answer as soon as the update arrives, before any slow work.
Why do I see it after a restart or deploy?
Telegram keeps updates for up to 24 hours until the bot receives them. Buttons pressed while the bot was down arrive all at once when it starts again, and by then they are too old to answer. The error stops once the backlog is worked through.
Is it safe to ignore this error?
Yes, for answerCallbackQuery on a press that is already stale: the user has stopped waiting, and nothing you send now will show. Catch only this exact error and raise the rest. If you see it on fresh presses, the handler is answering too late and should answer first.
Should I use drop_pending_updates?
Only if losing the backlog is fine. drop_pending_updates throws away every pending update, messages and commands included, not just old button presses. It suits bots whose buttons only make sense for a short time; bots that take orders or payments should rather process the backlog and ignore this one error.
Does the error mean the button did not work?
Not necessarily. Only the answer failed, so the user saw the loading indicator run out instead of a notification. The rest of your handler, such as editing the message, can still run if you catch the error or answered before the slow part.
Sources
- Telegram Bot API — Making requests, Getting updates, getUpdates, setWebhook, deleteWebhook, CallbackQuery, answerCallbackQuery (core.telegram.org). Checked .
- Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
- grammY — Keyboards (Inline and Custom Keyboards) (grammy.dev). Checked .
- aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
- aiogram source — aiogram/client/session/base.py (raw.githubusercontent.com). Checked .
- aiogram — Callback answer (docs.aiogram.dev). Checked .
- aiogram source — aiogram/utils/callback_answer.py (raw.githubusercontent.com). Checked .
- aiogram source — aiogram/methods/delete_webhook.py (raw.githubusercontent.com). Checked .
- aiogram source — aiogram/dispatcher/dispatcher.py (raw.githubusercontent.com). Checked .
- python-telegram-bot source — telegram/error.py (raw.githubusercontent.com). Checked .
- python-telegram-bot source — telegram/request/_baserequest.py (raw.githubusercontent.com). Checked .
- python-telegram-bot source — telegram/_callbackquery.py (raw.githubusercontent.com). Checked .
- python-telegram-bot source — telegram/ext/_application.py (raw.githubusercontent.com). Checked .
- grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
- grammY source — src/bot.ts (raw.githubusercontent.com). Checked .
- grammY source — src/context.ts (raw.githubusercontent.com). Checked .
- Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .
- Telegraf source — src/telegraf.ts (raw.githubusercontent.com). Checked .
- Telegraf source — src/context.ts (raw.githubusercontent.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your botRelated
- Bad Request: message is not modified — Telegram bot error and how to fix itBad Request: message is not modified means your bot asked Telegram to edit a message into exactly what it already shows: the same text, caption and inline keyboard. It usually comes from a user pressing the same button twice or a refresh button with nothing new. Skip the edit when nothing changed, or catch this exact error and ignore it.
- Conflict: terminated by other getUpdates request — how to fix itTelegram is telling your bot that another process called getUpdates with the same token and took the connection over. Two copies of the bot are polling at once: stop the other one (usually the copy still running on your computer or an old server), or get a new token from @BotFather, and the error stops.
- Telegram 429: Too Many Requests, flood control: retry after N — how to fix itTelegram flood control (429 Too Many Requests) is telling your bot to slow down: wait the number of seconds in retry_after, then send the same request again. It comes from sending too fast — more than about one message a second to one chat, 20 a minute to a group, or about 30 a second in a broadcast. Wait and retry in code, and send broadcasts from one queue at a steady pace.
- Bad Request: can't parse entities — Telegram bot error and how to fix itBad Request: can't parse entities means Telegram could not read the formatting in your message: with parse_mode set to HTML, Markdown or MarkdownV2, some text — usually a user's name, a username with an underscore, a price with a dot, or an unclosed tag — breaks the markup. Escape that text for the parse mode you use, send entities instead of parse_mode, or send it without parse_mode.
- How to keep a Telegram bot running 24/7A bot run with python bot.py or node bot.js stops when the terminal closes, the laptop sleeps or the code crashes, and two copies polling one token knock each other off with 409 Conflict. Keep exactly one copy running under something that restarts it — systemd, Docker or a platform — and stop it cleanly on SIGTERM. On Livops a bot runs from a push for 19 credits a month (19,000 so'm).
- Telegram bot hosting in 2026: the options comparedA Telegram bot needs a process that never stops, or a public HTTPS address for a webhook. On Livops it runs from a GitHub push for 19 credits a month (19,000 so'm). Here is how a VPS, Railway, Render, Fly.io and Vercel compare.
- aiogram bot hosting: how to deploy an aiogram 3 botAn aiogram 3 bot needs one process that never stops, the token in an environment variable, state kept outside memory and a clean exit on SIGTERM. On Livops you push a repository with bot.py and requirements.txt, and it runs for 19 credits a month (19,000 so'm), polling or webhook.
- grammY bot deploy: polling, webhooks and hostingDeploy a grammY bot as one always-on Node.js process: bot.start() for polling, or webhookCallback() behind an HTTP server, with bot.stop() on SIGTERM. On Livops a repository with grammy in package.json runs as a bot from every push, for 19 credits a month (19,000 so'm).