Streaming Telegram Agent
OpenAI agent with token-by-token streaming via live message edits
Code
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIChat4from kern.os.app import AgentOS5from kern.os.interfaces.telegram import Telegram67agent_db = SqliteDb(8 session_table="telegram_sessions", db_file="tmp/telegram_streaming.db"9)1011telegram_agent = Agent(12 name="Telegram Streaming Bot",13 model=OpenAIChat(id="gpt-4o-mini"),14 db=agent_db,15 instructions=[16 "You are a helpful assistant on Telegram.",17 "Keep responses concise and friendly.",18 "When in a group, you respond only when mentioned with @.",19 ],20 add_history_to_context=True,21 num_history_runs=3,22 add_datetime_to_context=True,23 markdown=True,24)2526agent_os = AgentOS(27 agents=[telegram_agent],28 interfaces=[29 Telegram(30 agent=telegram_agent,31 reply_to_mentions_only=True,32 streaming=True,33 )34 ],35)36app = agent_os.get_app()3738if __name__ == "__main__":39 agent_os.serve(app="streaming:app", reload=True)Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateSet Environment Variables
1export TELEGRAM_TOKEN=your-bot-token-from-botfather2export OPENAI_API_KEY=your-openai-api-key3export APP_ENV=developmentInstall dependencies
1uv pip install -U "kern-ai[telegram]"Run Example
1python streaming.pyKey Features
- Token-by-Token Streaming: Responses appear incrementally as they are generated
- Live Message Edits: The bot edits its message in real time, throttled to stay within Telegram rate limits
- Conversation History: Maintains context with last 3 interactions
- Persistent Memory: SQLite database for session storage