- Telegram holds your bot messages.
- The daemon (a small bash script running in the background) asks Telegram "any new message?" and receives the text.
- claude -p runs Claude Code in headless mode: one instruction, no interactive window, with the dedicated system prompt.
A Telegram bot and a dedicated Claude Code sub-agent are not the same thing: a regular connection gives you general control from your phone, but a dedicated sub-agent is a daemon that runs claude -p in headless mode with a system prompt locked to one purpose. It does one job exceptionally well, not a chat for everything.
To turn Claude Code into a dedicated sub-agent on Telegram, you set up a small daemon that runs in the background using long-polling: it listens for messages on a Telegram bot, and for each message it runs claude -p in headless mode with --append-system-prompt to lock the agent to one fixed purpose, restricts its tools with --allowedTools, and sends the response back to the chat. The bot is locked to your chat id only, and the daemon always runs under launchd (on macOS) or systemd (on Linux). The result: one bot that does one job, and does it well.
Disclosure: this is a direct continuation of Connect Claude Code to Telegram. There we built a general-purpose chat; here we lock it to a single role.
What is a "dedicated sub-agent" and why is it powerful
Connecting Claude Code to Telegram in the standard way gives you an all-purpose chat: you send "publish the article," "check status," "run tests," whatever comes to mind. That's great, but it also means you have to think and compose a request every single time.
A dedicated sub-agent flips that: one bot, one purpose. Instead of an open chat, you have a focused worker: "lead-filtering agent," "daily-report agent," "content-ideas agent." You throw raw input at it, and it already knows what to do with it, because its role is locked. The advantages:
- Consistency: the agent always behaves according to the same role, without drifting and without you needing to remind it who it is.
- Safety: an agent that only reads and summarizes never gets permission to modify files. You tailor the permissions to the task.
- Ease of use: no need to craft the perfect prompt every time. Send text, receive output in a fixed format.
- Multiple agents: you can run several bots in parallel, each with its own role, without them interfering with each other.
The architecture in short
The flow is simple and linear:
Telegram → daemon (long-polling) → claude -p → response → Telegram
- Telegram holds your bot messages.
- The daemon (a small bash script running in the background) asks Telegram "any new message?" and receives the text.
claude -pruns Claude Code in headless mode: one instruction, no interactive window, with the dedicated system prompt.- The output goes back to the chat. The daemon goes back to listening. That's it, an infinite loop.
Unlike the standard setup where you manage the loop yourself, here the daemon is an independent component that always runs in the background. This is exactly the principle from Scheduled Automation for Claude Code: headless plus a scheduler, except here the trigger is a Telegram message rather than a clock.
How to open a Telegram bot, step by step
This is the practical step where many people get stuck. Here is every step:
- Open BotFather. In Telegram, search for
@BotFather(Telegram's official bot for creating bots, with a blue checkmark). Press Start. - Create a new bot. Send
/newbot. - Choose a display name. For example
Leads Filter. This is the name that appears in the chat. - Choose a username. It must be unique and end in
bot, for examplemy_leads_filter_bot. - Receive the token. BotFather will return a token that looks like this:
8927:AAH.... This is the key to the entire API, treat it like a password. Save it and do not share it. - Find your chat id. Send any message to the new bot (for example "hi"), then open in a browser:
https://api.telegram.org/bot<your-token>/getUpdatesIn the JSON response you will see"chat":{"id":123456789,...}. That number is yourchat id. - Lock the bot to yourself. Save that
chat id, and from that point on the daemon will ignore any message that did not come from it. This way nobody else can activate your agent.
Save both pieces of information in a local file, not in code and not in git:
# ~/.telegram-agent-creds
TG_TOKEN=the-token-you-got-from-BotFather
TG_CHAT=123456789 # your chat id from getUpdates
chmod 600 ~/.telegram-agent-creds # only you can read this
If the token ever leaks, /revoke in BotFather generates a new token and invalidates the old one.
The agent's brain: --append-system-prompt
This is the heart of the dedicated sub-agent. Instead of explaining to Claude what to do with every message, you attach a fixed role to it using --append-system-prompt. Here is the runner line the daemon executes for every message:
REPLY="$(claude -p "$MSG" \
--append-system-prompt "You are a lead-filtering agent. Read the message, classify the lead as 'hot', 'warm', or 'cold' based on purchase intent, and return exactly one line: the classification and a brief reason. Do not explain the process." \
--allowedTools "Read" \
--max-turns 4)"
What is happening here:
"$MSG", the text the user sent on Telegram, comes in as the prompt.--append-system-prompt, the fixed role instruction. It is attached to every run, so the agent always filters leads regardless of how the message is phrased.- The output (
$REPLY) is sent back to Telegram viasendMessage.
Want a different agent? Change only the system prompt string. The exact same infrastructure, a different role.
Read-only vs. full access: --allowedTools
This is where you decide how much power the agent has, and that is a security decision, not a convenience one. --allowedTools restricts exactly which tools Claude Code is allowed to use:
- Read-only agent (summarizing, filtering, classifying):
--allowedTools "Read"or--allowedTools "Read" "Bash(git log:*)". It can read and report, but not write, edit, or delete. This is the safe default for most agents. - Full-access agent (publishing, updating files, running deployments):
--allowedTools "Read" "Edit" "Write" "Bash". Give it only what it genuinely needs for the task, not everything "just in case."
Two golden rules:
- Start locked, open only when needed. If the agent only reports, do not give it write tools at all.
- The bot is locked to one chat id. Even if the agent is powerful, only you can activate it. Combining a narrow
--allowedToolswith a locked chat id is your double layer of protection.
Cumulative memory: ledger
An agent that runs repeatedly benefits a lot from memory. Instead of starting from scratch with every message, keep a simple log file (a ledger) that the agent reads and updates:
LEDGER="${HOME}/.leads-agent-ledger.md"
REPLY="$(claude -p "New lead: $MSG" \
--append-system-prompt "You are a lead-filtering agent. Read the ledger at $LEDGER before classifying, and write one line about the new lead (date, classification, reason) to it at the end." \
--allowedTools "Read" "Edit($LEDGER)" \
--max-turns 6)"
Now the agent remembers previous leads, detects duplicates, and learns over time, with no database, just one Markdown file. Note: here we gave write permission only to the ledger file (Edit($LEDGER)), not to the entire project.
Always running: launchd / systemd
A daemon sitting in an open terminal is not reliable: close the window and everything stops. The right approach is to let the operating system run it and revive it if it crashes:
- macOS, launchd. Create a
LaunchAgentfile withKeepAlive=true, so the daemon restarts automatically if it crashes and also after a reboot. - Linux, systemd. Create a unit with
Restart=always.systemctl --user enableensures it always runs in the background.
This is exactly the same independent-execution principle we covered in Scheduled Automation for Claude Code, except here the daemon runs continuously and listens, rather than waking up at a fixed time. The ready-made launchd and systemd files, along with the full daemon, are collected in the sub-agent kit.
Three examples of dedicated agents
Same infrastructure, three roles, the entire difference is the system prompt and --allowedTools:
- Transcript scanner, content ideas. You send a transcript of a meeting or podcast episode; the system prompt: "Read the transcript, extract 5 post ideas with a headline and angle." Read-only. You receive ready-made ideas in Telegram.
- Lead filter. You send inquiry details; the system prompt: "Classify as 'hot/warm/cold' based on purchase intent, return one line." Read-only plus ledger for duplicate detection. Saves the manual sorting.
- Daily report. In the morning you send "report"; the system prompt: "Read yesterday's logs and metrics and return a 4-line summary." Read-only plus Bash restricted to reading. A daily metric without opening a dashboard.
The bottom line
A dedicated sub-agent in Claude Code is a small long-polling daemon that listens to a Telegram bot and runs claude -p headless with --append-system-prompt to lock it to one purpose, --allowedTools to set how much power it has, and a lock to your chat id. Open the bot in BotFather, write the daemon once, and run it permanently with launchd or systemd. One bot, one job, done.
Want to learn how to build and operate Claude Code agents? There is Claude in Hebrew, the central guide, the full toolkit in the sub-agent kit, and a 1-on-1 private lesson where we build your dedicated agent together, on your real project.
Frequently asked questions
What is the difference between a regular Telegram connection and a dedicated sub-agent?
A regular connection is a general-purpose chat that handles any task. A dedicated sub-agent is one bot with one locked purpose (--append-system-prompt): consistent, safe, and straightforward to operate. One bot, one job.
How do I open a bot and get a chat id?
/newbot to BotFather, choose a name and a username ending in bot, receive the token. Then send a message to the bot and open getUpdates: your chat id appears in the JSON. Lock the daemon to it.
Read-only or full access?
Start read-only (--allowedTools "Read") for every agent that only reads and reports. Add write access or Bash only when the task genuinely requires it, and only for what is needed, for example Edit($LEDGER) alone.
How do I make it always run?
launchd with KeepAlive=true on macOS, or a systemd unit with Restart=always on Linux. The daemon starts after a reboot and recovers if it crashes.
What agents like this are worth building? We put together 5 ready-made recipes for AI workers in Telegram for your business: lead filter, content scout, morning report, document summarizer, and quote checker, each with its full prompt.

