Streaming Telegram Agent

OpenAI agent with token-by-token streaming via live message edits

Code

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIChat
4from kern.os.app import AgentOS
5from kern.os.interfaces.telegram import Telegram
6
7agent_db = SqliteDb(
8 session_table="telegram_sessions", db_file="tmp/telegram_streaming.db"
9)
10
11telegram_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)
25
26agent_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()
37
38if __name__ == "__main__":
39 agent_os.serve(app="streaming:app", reload=True)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set Environment Variables

1export TELEGRAM_TOKEN=your-bot-token-from-botfather
2export OPENAI_API_KEY=your-openai-api-key
3export APP_ENV=development

Install dependencies

1uv pip install -U "kern-ai[telegram]"

Run Example

1python streaming.py

Key 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