Telegram Agents — Deploy Your Own AI Stack
Set up Claude Code in tmux + Telegram. Your agent runs while you sleep.
See it work
Placeholder — real preview will be a 30s screen recording: you DM your Telegram bot a task, the agent works in tmux, replies with the result.
Is this for me?
Yes, if you want an AI agent that lives on your laptop, listens to your Telegram messages 24/7, and quietly executes tasks (research, intel scans, content drafts, code commits) while you do other things. Once set up, you message it like a person. It replies like a person. Except it never sleeps and it works for the cost of an Anthropic subscription.
What you need
- A Mac, Linux box, or Windows w/ a Linux-style terminal
- Claude Code (free CLI from Anthropic — Step 1 walks you through install)
- An Anthropic Pro or Max account (Pro is fine to start; Max if you plan long autonomous sessions)
- A Telegram account (free)
- tmux (free terminal multiplexer — Step 2 walks you through install)
- About 45 minutes of focused time
Zero code typed by you. Every command in this module is paste-and-enter. We walk you through every install, every paste, every check.
Do it
- step 1 / 6
Install Claude Code
If you've already got Claude Code running, jump to Step 2.
Open the Terminal:
- Mac:
⌘ + Space→ typeterminal→ enter. - Windows: Windows key → type
terminal→ enter. - Linux: You know where it is.
Paste this one line, hit enter:
curl -fsSL https://claude.ai/install.sh | bashText scrolls for 30-60 seconds. When the cursor comes back, it's installed.
Verify:
claudeFirst run it asks you to sign in with your Anthropic account. Pro plan handles this module; Max plan is better if you'll run long autonomous sessions.
Done. You now have Claude Code on your machine. Move to Step 2.
- Mac:
- step 2 / 6
Install tmux + start a session
What is tmux? A terminal multiplexer — fancy way of saying "one terminal that can hold multiple sessions, and those sessions keep running even after you close the terminal window." That's the magic. You start a Claude session in tmux, close your laptop, the agent keeps running on the box, you re-open later and it's still there.
Install tmux:
- Mac:
brew install tmux(if you don't have brew yet, install it from https://brew.sh first) - Linux:
sudo apt install tmuxorsudo dnf install tmuxdepending on distro - Windows: Use WSL (Windows Subsystem for Linux), then
sudo apt install tmux
Start a named session for your agent:
tmux new -s agentYou're now inside a tmux session called
agent. Anything you do here keeps running even after you detach.Detach (leave the session running): Press
Ctrl + b, thend. Re-attach later:tmux attach -t agent.Leave a Claude Code session running in this tmux window — that's your always-on agent. It only goes down if your machine sleeps or reboots.
- Mac:
- step 3 / 6
Register a Telegram bot with BotFather
Open Telegram. Search for
@BotFather. Open the chat.Send:
/newbotIt asks for a display name (members see this — try "Loya's Agent" or whatever). Then asks for a username (must end in
bot— tryloya_agent_bot).BotFather replies with a token that looks like
8123456789:ABCdEFGhIJKlmnOPqRsTuVwXyZ-1234567890. Save that token somewhere safe — it's the only thing standing between you and someone hijacking your bot.Create a file at
~/.claude/agent.envwith the token:mkdir -p ~/.claude echo 'BOT_TOKEN=8123456789:ABCdEFGh...' > ~/.claude/agent.env chmod 600 ~/.claude/agent.env(Replace the example token w/ the one BotFather gave you.)
Get your own Telegram chat ID: in Telegram, search for
@userinfobot, send/start, copy theId:number. Add it toagent.env:echo 'CHAT_ID=YOUR_NUMBER_HERE' >> ~/.claude/agent.envNow your agent knows which token to use AND which chat to post in.
- step 4 / 6
Wire the message bus
The pattern in plain English: your agent (running in tmux) needs two scripts — one that SENDS a message to your Telegram, and one that RECEIVES messages you send back to it. That's the bus.
Send-telegram script
Create the send script:
mkdir -p ~/.claude/bin cat > ~/.claude/bin/send-telegram.sh <<'EOF' #!/usr/bin/env bash # Usage: send-telegram.sh "<message>" set -euo pipefail source ~/.claude/agent.env curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \ -d chat_id="${CHAT_ID}" \ --data-urlencode "text=$1" >/dev/null EOF chmod +x ~/.claude/bin/send-telegram.shTest it:
~/.claude/bin/send-telegram.sh "hello from your agent"If you see that message land in Telegram, the send-half works. Your agent can now ping you any time.
Receive-telegram pattern
This one's a daemon — it polls Telegram for new messages from you and forwards them into a place your agent can read.
The simplest version: a script that runs
getUpdates, dumps each new message into~/.claude/inbox/<msgid>.txt, and your agent reads that directory at the start of each fire.The more durable version (what we run internally): a long-running poll-daemon w/ exponential backoff + an offset cursor so messages don't replay. The pattern is documented in Module 8 (autonomous-agent-telegram) — copy it directly when you're ready.
For the absolute first deploy, skip the receive-half. Have your agent send-only first. Once you're sending pings reliably, layer in the receive-daemon.
- step 5 / 6
Drop in a starter skill
Skills are extensions that teach Claude Code new abilities — read a webpage cleanly, drive a browser, post to social, scrape X, anything. Each skill is a folder under
~/.claude/skills/with oneSKILL.mdfile telling Claude what it does + how to use it.For your first skill, pick defuddle — it converts any URL into clean markdown (no ads, no nav, no cookie banners). Smaller token bill, cleaner reading, instant value.
Install defuddle
cd ~/.claude/skills git clone https://github.com/kepano/defuddleThat's it. Next time you open Claude Code in any project, type:
Use the defuddle skill to read https://example.comIt'll auto-detect the skill, run it, and give you the article body in clean markdown — usually 60-80% smaller than what WebFetch would return.
Where to find more skills
The Armory (
/armoryon this site) is the curated index of every skill + tool worth running. browser-harness for real-Chrome control. postiz for cross-posting. seedance-video for AI clips. Pick whichever matches what you actually do.Write your own (advanced)
A SKILL.md is just a markdown file w/ a frontmatter
name+description+ a body explaining what the skill does and how to invoke it. Look inside the defuddle folder for a working example — copy + paste structure for your own. - step 6 / 6
Cost control
Three knobs control your Anthropic bill. Get them right and a 24/7 agent costs $20-50/mo. Get them wrong and it's $500+/mo.
Knob 1 — Model choice
Default to Claude Sonnet 4 for routine work. It's ~5x cheaper than Opus and good enough for 90% of agent tasks (intel scans, content drafts, simple code edits). Switch to Claude Opus 4 only for: long autonomous build loops, complex architecture decisions, anything where wrong-output costs more than the model price.
Flag in tmux when starting Claude Code:
claude --model claude-sonnet-4.Knob 2 — Context discipline
The single biggest cost driver is prompt cache hit rate. Anthropic's cache holds your prompts for 5 minutes; if your agent fires within that window, you re-use cached tokens at ~10% the cost. If it fires outside that window, full price.
Practical rule: set
/loopintervals at 20-30 min, NOT 5 min. A 5-min loop fires within the cache window every time → full price every time. A 20-min loop pays for the cache miss once per fire instead of 4x. Net savings: ~60% on long-running loops.Also: scope your skills tight. A skill that loads 30 files of context costs 30x what a skill loading 1 file costs. The Armory's curated entries are scoped intentionally — borrow that pattern.
Knob 3 — Session lifetime
Don't leave the agent in a 24/7 tmux session if it's only doing daily-cron work. The Anthropic Pro/Max subscription is fixed, but your CPU/network bill on a VPS isn't.
For most members: keep it on your laptop, only run the agent during your active work hours, let cron-driven crons fire only when needed. For pro members running revenue agents 24/7: deploy on a $5/mo VPS (Hetzner CX22 is the cheapest stable option).
Sanity check
At month's end, log into your Anthropic dashboard. Compare token usage to your daily activity. If you're surprised, your loop cadence is too tight or you're on Opus when Sonnet would do.
Make it yours
- Pick ONE recurring task you do every day or week — research a topic, draft a post, scan a feed, organize notes. That's your agent's first job.
- Tell the agent: 'Every morning at 9am, scan [these 3 X accounts / this subreddit / this RSS feed], summarize what's new, ping me on Telegram.' Use the autonomous-build-loop module's cron pattern.
- Want it to ship code while you sleep? Add a BUILD_PLAN.md to a project, point the agent at it, set the loop to 30 min. Wake up to commits + screenshots.
- Want it to be a content drafter? Drop in defuddle + a writing skill. Tell it: 'When I send a URL, read the article, draft a 3-tweet thread in my voice.' Reply with raw URLs from your phone.
Stuck?
- tmux session disappears after I close my laptop lid.
Mac sleep kills tmux processes by default. Either leave the laptop open + plugged in, or run `caffeinate -d` in a terminal before sleeping. Better: deploy the agent on a $5/mo VPS (Hetzner CX22, DigitalOcean droplet) so it actually runs 24/7.
- BotFather replies but I forgot to save the token.
Send `/mybots` to BotFather, pick your bot, click 'API Token' — it shows the token again. If you suspect it leaked, click 'Revoke current token' and generate a fresh one (anyone with the old one had bot access).
- Claude Code asks me to sign in every session.
Your auth token isn't persisting. Run `claude auth login` once outside tmux first to seed it, then start the tmux session. The auth lives in `~/.claude/auth.json`.
- Telegram messages from the bot don't arrive.
Either the bot's token is wrong (re-paste from BotFather), the chat_id is wrong (re-check via @userinfobot), or you haven't sent the bot a message first. Telegram blocks bots from messaging users until the user initiates the chat. Send your bot any message, then retry.
- Anthropic billing surprised me.
Switch the agent to Sonnet 4 for routine work (quarter the cost of Opus). Use /loop intervals of 20-30 min, not 5. Cap context at 100k tokens via the skill scope. Most surprises come from running Opus on a 5-min loop with no context discipline.
- 2026-04-25v0.2 — all 6 doIt sections fleshed (Claude Code install / tmux / BotFather / message bus / starter skill / cost control). Headline offer module complete; preview screen-rec pending.
- 2026-04-25v0.1 — module scaffolded. Sections 1-3 fleshed; sections 4-6 stubbed.
Next up
Your Telegram agent is now alive on your machine. Module 12 (Autonomous Build Loop) teaches the framework that lets it ship CODE on its own — BUILD_PLAN, BUILD_STATE, the 30-min cron, the auto-pause rules. This is how this whole site got built without Loya typing a line.