Skip to main content

Code

multiple_instances.py
import os

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIChat
from agno.os.app import AgentOS
from agno.os.interfaces.telegram import Telegram
from agno.tools.websearch import WebSearchTools

agent_db = SqliteDb(db_file="tmp/persistent_memory.db")

basic_agent = Agent(
    name="Basic Agent",
    model=OpenAIChat(id="gpt-5.2"),
    db=agent_db,
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
    markdown=True,
)

web_research_agent = Agent(
    name="Web Research Agent",
    model=OpenAIChat(id="gpt-5.2"),
    db=agent_db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
)

# Telegram only supports one webhook per bot token, so each interface needs its own bot.
# Create two bots via @BotFather and pass each token explicitly.
agent_os = AgentOS(
    agents=[basic_agent, web_research_agent],
    interfaces=[
        Telegram(agent=basic_agent, prefix="/basic", token=os.getenv("TELEGRAM_TOKEN_BASIC")),
        Telegram(agent=web_research_agent, prefix="/web-research", token=os.getenv("TELEGRAM_TOKEN_RESEARCH")),
    ],
)
app = agent_os.get_app()

if __name__ == "__main__":
    agent_os.serve(app="multiple_instances:app", reload=True)

Usage

1

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate
2

Set Environment Variables

export TELEGRAM_TOKEN_BASIC=bot-token-for-basic-agent
export TELEGRAM_TOKEN_RESEARCH=bot-token-for-research-agent
export OPENAI_API_KEY=your-openai-api-key
export APP_ENV=development
3

Install dependencies

uv pip install -U "agno[telegram]"
4

Run Example

python multiple_instances.py
5

Register Webhooks

Register a webhook for each bot token:
curl "https://api.telegram.org/bot${TELEGRAM_TOKEN_BASIC}/setWebhook?url=${NGROK_URL}/basic/webhook"
curl "https://api.telegram.org/bot${TELEGRAM_TOKEN_RESEARCH}/setWebhook?url=${NGROK_URL}/web-research/webhook"

Key Features

  • Prefix-Based Routing: Each agent gets its own webhook path (/basic/webhook, /web-research/webhook)
  • Shared Server: Both agents run on a single AgentOS instance
  • Separate Bot Tokens: Each interface uses its own BotFather bot (Telegram allows only one webhook per token)
  • Shared Database: Both agents share the same SQLite database