The fifty-line bot

An AI bot is, at its most reduced, three things: a way to receive a message, a way to call a model, and a way to send the reply back. Nothing more. Everything else we add later is a refinement on this triangle.

01-simplest-bot/bot.py
python
import os
import sys

from dotenv import load_dotenv
from openai import OpenAI
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes

load_dotenv(override=True)

MODEL = os.environ.get("OPENROUTER_MODEL", "qwen/qwen3-coder")
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.environ["OPENROUTER_API_KEY"],
)


def ask(user_text: str) -> str:
    """One stateless call to the LLM."""
    response = client.chat.completions.create(
        model=MODEL,
        max_tokens=1024,
        messages=[{"role": "user", "content": user_text}],
    )
    return response.choices[0].message.content or ""

The whole bot. Take a moment to read it top to bottom. There is no hidden cleverness anywhere.

The OpenAI Python SDK is doing all the HTTP work for us. base_url makes it talk to OpenRouter instead of OpenAI. The messages array is the entire conversation, which here is a single user turn. response.choices[0].message.content is where the reply lives.

It doesn't. Every call sends a fresh messages array. The OpenRouter API is stateless, the provider stores nothing between calls. If you want memory, you stuff it into the next messages array yourself. That is exactly what the JSONL session work adds.

01-simplest-bot/bot.py
python
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    reply = ask(update.message.text)
    await update.message.reply_text(reply)


def run_telegram():
    token = os.environ["TELEGRAM_BOT_TOKEN"]
    app = ApplicationBuilder().token(token).build()
    app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
    app.run_polling()


def run_cli():
    while True:
        try:
            user_text = input("you> ").strip()
        except (EOFError, KeyboardInterrupt):
            break
        if user_text in {"exit", "quit"}:
            break
        if user_text:
            print(f"bot> {ask(user_text)}")

Two channels share the same ask() function. Telegram is just one of them. CLI is the default so you don't need a bot token to play.

Run it once and watch the round trip. Then try the same message twice and see that the bot has no clue you said it before.

terminal
bash
make 01-simplest-bot

# you> what is 2 + 2?
# bot> 4
# you> what did I just ask?
# bot> I don't have memory of previous messages...

make 01-simplest-bot runs in CLI mode by default. Pass --telegram if you want to hook up the bot token.

Quiz: Quiz

Loading practice…

AI prompt: Try it: change the brain

Loading practice…