Build Your Own AI Agent: Step-by-Step Guide with Claude Code & Telegram
This guide defines AI agents as a spectrum, provides system prompt templates for research, writing, coding, and planning agents, and details building a Telegram bot with Claude Code including persistent memory, web search, cost tracking, and systemd deployment.
What Counts as an Agent
Everyone is talking about AI agents, but most people give a vague answer: "AI that can do things autonomously." The reality is more useful. An agent is not a fixed category but a spectrum.
From Chat to Agent
This guide explains where different AI tools sit on that spectrum, the difference between basic LLM interaction and true agentic workflows, and how to build your own agent with Claude Code and connect it to Telegram without writing code yourself.
Agent Types You Can Build Today
Writing Agent: Writes content following your defined system. Give it your tone, format, audience, and topic. It handles drafting, rewriting, and editing without you managing every sentence.
Code Agent: Writes code, runs it, reads errors, fixes them, and keeps going. You describe what the code should do; the agent handles the implementation and debugging loop.
Business Agent: Handles repetitive business tasks: drafting emails, processing customer requests, qualifying leads, generating reports. Once you define the rules, it runs automatically.
Personal Agent: Manages your calendar, organizes tasks, prepares briefings, and handles the planning work you do manually every day.
Build Your First Agent — Start Here
System Prompt Templates — Paste into Prompt 2
When you run Prompt 2 in the setup below, you describe the agent you want. Below are ready-made templates; copy the one that fits, paste it where Prompt 2 says "The agent should be:", and Claude Code will build that agent. Adjust details for your situation.
Research Agent
You are a research agent. Your job is to gather, analyze, and synthesize information around any topic I give you.
When you receive a research task:
1. Identify the 3-5 most important sub-questions that need answering
2. Search for information on each sub-question
3. Evaluate the quality and relevance of each source
4. Extract only the information that directly answers the questions
5. Provide a structured summary with: key findings, supporting evidence, and gaps that could not be filled
Format: Use headings, bullet points, and clear sections.
If you are unsure about something, say so explicitly.
No fluff. Every sentence should carry information.Writing Agent
You are a writing agent. You write content in my voice and style.
My style:
- Conversational, direct, no corporate speak
- Short sentences and paragraphs
- Use concrete numbers and examples, not vague claims
- Always end with something the reader should do or think about
When you receive a writing task:
1. Ask for topic, audience, and desired length if not provided
2. Write a first draft
3. Review it against my style rules
4. Deliver a final version ready to publish
No unnecessary introductions. Start with the most important point.Code Agent
You are a code agent. Your job is to write, debug, and improve code.
When you receive a coding task:
1. Confirm you understand what the code should do
2. Write the solution with clear comments
3. Identify any edge cases or potential failure points
4. If errors appear, debug systematically before asking for help
Rules:
- Write clean, readable code with meaningful variable names
- Always include error handling
- Explain what each major section does in plain language
- If you are unsure about a requirement, ask one clarifying question before proceedingBusiness Email Agent
You are a business email agent. You write professional emails on my behalf.
My communication style:
- Direct and respectful
- No unnecessary pleasantries
- State the point in the first sentence
- End with a clear next step
When you receive an email task:
1. Identify the goal of this email (inform, request, follow up, confirm)
2. Write a subject line that reflects the purpose
3. Draft the email, max 3-5 short paragraphs
4. End with a clear action item or next step
Always write emails that are ready to send, not templates with blanks.Personal Planning Agent
You are my personal planning agent. You help me organize work, prioritize tasks, and plan my week.
When I share tasks or goals:
1. Separate what is urgent from what is important
2. Suggest a realistic order based on dependencies
3. Estimate time needed for each task
4. Flag anything that can be delegated or dropped
When I describe a project:
- Break it into concrete next actions
- Identify the single most important first step
- Create a simple checklist I can follow
Keep everything practical. No motivational fluff. Just the plan.What You Need Before You Start
Which Claude model your bot should use and what it actually costs:
For most personal bots, Sonnet 4.6 is the right choice — powerful enough for any task, suitable for daily use, and reasonably priced. If you want to minimize cost, use Haiku 4.5. Only Opus 4.8 makes sense when building complex analytical agents where answer quality is critical.
Build Your First Agent — Step 1
Help me build a Telegram bot that uses the Claude API as its brain.
Requirements:
- Language: Python
- Telegram library: python-telegram-bot
- Claude model: claude-sonnet-4-6
- Bot receives a message, sends it to Claude API, returns Claude's reply to Telegram
- Maintain conversation history per user — each user has their own context in the session
- Add a /start command that introduces the bot
- Add a /clear command that resets that user's conversation history
Create all necessary files: main bot file, requirements.txt, and a .env file template for API keys.
Do not hardcode any API keys — read from environment variables.Deploy as a Background Service
Create a systemd service file so this bot runs automatically on my Linux VPS and restarts on crash.
The service should:
- Start automatically on server boot
- Restart automatically on crash
- Load environment variables from the .env file in the project folder
- Save logs to a file I can check later
Also provide the exact terminal commands to install the service, start it, check if it's running, and view logs.Add Persistent Memory Across Sessions
The bot loses conversation history on restart. Fix this.
After every message, save each user's conversation history to a JSON file on disk. Load it automatically on startup.
Add a maximum history length — keep only the last 20 messages per user so the context window doesn't grow too large.Add Skills to Your Claude Code Bot
Skills are capabilities you add to the bot over time. Each skill is a new prompt you give Claude Code; it writes the code to implement it. Here are the most useful ones.
Web Search with Tavily API
Add web search capability to the bot using the Tavily API.
When a user asks a question that needs current information — news, prices, recent events — the bot automatically searches the web and includes the results in its reply.
The bot should decide on its own when to search and when to answer from memory. Add TAVILY_API_KEY to the .env file template.
Use Claude's tool use feature to implement this cleanly.Save Notes
Add a note-saving feature to the bot.
When a user says "save this", "remember this", or "note:", the bot saves the content with a timestamp to a notes.txt file.
Add a /notes command that returns the last 10 saved notes.
Add a /clearnotes command that deletes all saved notes.User Restrictions (Private Bot)
Add a user restriction so only I can use the bot.
Add my Telegram user ID as ALLOWED_USER_ID to the .env file.
If anyone else messages the bot, it replies "This bot is private." and ignores all further messages from that user.
Also add instructions for how to find your own Telegram user ID — both via the bot itself and other methods.Basic Cost Tracking
Add basic cost tracking to the bot.
After every Claude API reply, log the input tokens and output tokens used.
Keep a running total in a costs.json file.
Add a /costs command that shows: total tokens used today, all-time total tokens, and estimated USD cost based on claude-sonnet-4-6 pricing.Scheduled Daily Briefing
Add a scheduled daily briefing to the bot.
Every morning at 8:00 AM, the bot sends me a message with:
1. One motivational quote (short, not cheesy)
2. A reminder to check my top priority for the day
3. A random practical fact about productivity or AI
Send to my Telegram user ID in the .env file.
Use the schedule or APScheduler library to implement this.Useful Things to Know After Setup
How to update your agent's personality. Open the main bot file, find the system prompt variable at the top, modify it, save the file, then restart the service with sudo systemctl restart your-bot-name . The new personality takes effect immediately.
How to check if the bot is running. Run sudo systemctl status your-bot-name in the terminal. If it shows "active (running)", it's working. If it shows "failed", check the logs.
How to read logs. Run journalctl -u your-bot-name -n 50 to see the last 50 lines. This is where you look for error messages when something goes wrong.
How to add new skills. Open Claude Code in the project folder and describe what you want: "Add X feature to the bot." Claude Code reads your existing code, adds the new feature cleanly, and tells you if a restart is needed.
The Memory Problem Every Agent Has
This is the issue people hit most often. Agents lose context between sessions, during long tasks, and after too many messages, then start making mistakes or repeating work already done.
Checkpoint Prompt
Before we continue, write a brief checkpoint:
1. What have you accomplished so far?
2. What are the key decisions or findings from this session?
3. What still needs to be done?
4. If you had to pick up this task in a new session, what context would you need?
Keep it under 200 words.Memory File Prompt
After each major step, write a memory note in this format:
STEP COMPLETED: [what was finished]
KEY DECISIONS: [what choices were made and why]
CURRENT STATE: [where the task stands right now]
NEXT STEP: [what should happen next]
This note will be used to resume the task in a new session without losing context.Context Recovery Prompt
We are resuming a task from a previous session. Here is the context:
[paste your memory notes here]
Based on this:
1. Confirm you understand where we are
2. Identify the next step
3. Ask any questions needed to proceed
Do not repeat work already done.Rolling Summary Prompt
The conversation has gotten long. Compress everything important into a summary:
1. The original goal
2. What has been completed and what was discovered
3. Key decisions made
4. What still needs to happen
Write the summary, then continue from here. Treat this summary as the new starting point.Now you know what agents are, what they can do, and how to build one yourself. Hope this helps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
