Tutorial

How to Build an AI Agent (2026)

Step-by-step tutorial for building your first AI agent. From zero to a working autonomous agent with memory, tools, and scheduling.

March 25, 2026 · 5 min read

I built my first AI agent in 2025. It was terrible. It forgot everything between sessions, couldn't execute basic tasks, and broke every time I changed the prompt. I rebuilt it four times before landing on an architecture that actually works.

You don't need to make the same mistakes. This tutorial walks you through building a functional AI agent from scratch — one that remembers conversations, executes real tasks, operates on a schedule, and gets better over time. No theoretical framework discussions. No "here's how agents work conceptually." Just code, configuration, and a working agent at the end.

Try MrDelegate — Get your own OpenClaw assistant, fully hosted and managed. Start free trial →

What You're Building

By the end of this tutorial, you'll have an AI agent that:

  • Runs 24/7 on a server you control
  • Connects to Telegram (or Discord, or WhatsApp) for conversation
  • Remembers context across sessions with persistent memory
  • Executes shell commands, manages files, and searches the web
  • Runs tasks on a schedule without human prompting
  • Learns from its mistakes through structured memory

This isn't a toy demo. This is the same architecture that runs our company at MrDelegate — where every employee is an AI agent handling real production workloads.

Time required: About 1 hour for the basic setup. Another hour to customize and extend.

Cost: $6/month for a VPS + $5-30/month in AI model API costs depending on usage.

Step 1: Set Up Your Server

Your agent needs a machine that runs 24/7. Three options:

Option A: VPS (Recommended)

A $6/month VPS from Hetzner, Vultr, or DigitalOcean is the sweet spot. You get a dedicated machine with a public IP, root access, and enough resources for multiple agents.

Recommended specs: 2 vCPU, 4GB RAM, 40GB SSD, Ubuntu 24.04 LTS

# After SSH-ing into your new VPS:
apt update && apt upgrade -y
```bash

### Option B: Raspberry Pi

A Raspberry Pi 4 or 5 works for personal agents with moderate workloads. You'll need a way to expose it to the internet (Cloudflare Tunnel or Tailscale Funnel) for webhook-based channels.

### Option C: Your Existing Machine

If you already have a server, skip this step. OpenClaw runs anywhere with Node.js 22+.


## Step 2: Install OpenClaw

OpenClaw is the runtime that turns an AI model into an autonomous agent. It handles memory, tool access, scheduling, and channel management.

```bash
# Install Node.js 22
curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt install -y nodejs

# Verify
node --version  # Should be 22.x.x

# Install OpenClaw
npm install -g openclaw

# Initialize your workspace
openclaw init
```bash

The `init` command creates your workspace directory (typically `~/.openclaw/workspace/`) with the base configuration files.


## Step 3: Get an API Key

OpenClaw works with any AI model provider. I recommend starting with **Anthropic's Claude Sonnet** — it's the best balance of capability, speed, and cost for agent workloads.

1. Go to [console.anthropic.com](https://console.anthropic.com)
2. Create an account
3. Go to **API Keys** and create a new key
4. Add credits ($5-10 to start — this lasts weeks for moderate usage)

Alternatively:
- **OpenAI** — [platform.openai.com](https://platform.openai.com) for GPT-4
- **Google** — [aistudio.google.com](https://aistudio.google.com) for Gemini

Add your API key to OpenClaw:

```bash
# Edit the config
nano ~/.openclaw/openclaw.json
```bash

Add your model configuration:

```json
{
  "model": {
    "default": "anthropic/claude-sonnet-4-20250514",
    "provider": "anthropic",
    "apiKey": "sk-ant-your-api-key-here"
  }
}
```bash


## Step 4: Define Your Agent's Identity

This is where your agent becomes more than a generic chatbot. Create a `SOUL.md` file in your workspace:

```bash
nano ~/.openclaw/workspace/SOUL.md
```bash

```markdown
# Agent Identity

## Who You Are
You are Atlas, a personal AI assistant. You're direct, helpful, and efficient.
You don't waste words. You don't apologize unnecessarily. You get things done.

## What You Do
- Manage tasks and reminders
- Research topics and summarize findings  
- Draft emails, messages, and documents
- Monitor systems and alert when something needs attention
- Track projects and follow up on deadlines

## How You Communicate
- First person, conversational tone
- Specific over vague ("3 emails need replies" not "you have some emails")
- Proactive — suggest next steps, don't wait to be asked
- Honest about limitations — say "I can't do that" rather than hallucinating

## What You Don't Do
- Don't share private information in group chats
- Don't send emails or public posts without explicit approval
- Don't run destructive commands without asking first
- Don't make up information — search or say you don't know
```bash

This file gets loaded at the start of every session. The agent reads it and adopts the persona, capabilities, and boundaries you define. You can make it as simple or as detailed as you want.


## Step 5: Connect a Channel

Your agent needs a way to communicate. Telegram is the easiest to set up:

### Telegram Setup

1. Open Telegram and search for **@BotFather**
2. Send `/newbot`
3. Choose a name (e.g., "Atlas Agent") and username (e.g., "atlas_agent_bot")
4. BotFather gives you a token like `7123456789:AAG-example-token-here`
5. Add the token to your OpenClaw config:

```json
{
  "channels": {
    "telegram": {
      "enabled": true,
      "token": "7123456789:AAG-example-token-here"
    }
  }
}
```bash

### Start the Gateway

```bash
openclaw gateway start
```bash

Now message your bot on Telegram. If everything is configured correctly, your agent will respond. Congratulations — you have a working AI agent.

But right now it's just a chatbot. The next steps are what make it a real agent.


## Step 6: Set Up Persistent Memory

Without memory, your agent forgets everything between sessions. OpenClaw's memory system fixes this with a tiered architecture:

### Create the Memory Structure

```bash
mkdir -p ~/.openclaw/workspace/memory
```bash

Create `~/.openclaw/workspace/AGENTS.md` with the memory protocol:

```markdown
## Memory Protocol

### Session Start
On every new session:
1. Read SCRATCHPAD.md — current work state
2. Read WORKING.md — active tasks
3. Read memory/YYYY-MM-DD.md (today + yesterday) for recent context

### During Work
- Update WORKING.md when starting/completing tasks
- Write significant events to memory/YYYY-MM-DD.md
- Use SCRATCHPAD.md for temporary working notes

### Session End
- Update WORKING.md with task outcomes
- Clear completed items from SCRATCHPAD.md
- Write a summary to today's memory file
```bash

Create the initial working files:

```bash
echo "# Working Tasks\n\nNo active tasks." > ~/.openclaw/workspace/WORKING.md
echo "# Scratchpad\n\nClean slate." > ~/.openclaw/workspace/SCRATCHPAD.md
```bash

The agent now maintains state across sessions. When it starts a new conversation, it reads these files and picks up where it left off.


## Step 7: Add Skills

Skills teach your agent how to use specific tools. OpenClaw comes with several built-in skills, and you can install more from ClawHub or write your own.

### Install Community Skills

```bash
# Browse available skills
clawhub search weather

# Install a skill
clawhub install weather
```bash

### Write a Custom Skill

Create a skill that teaches your agent a specific capability. Example — a skill for checking server health:

```bash
mkdir -p ~/.openclaw/workspace/skills/server-health
```bash

Create `skills/server-health/SKILL.md`:

```markdown
# Server Health Check

## When to Use
Use this skill when asked to check server status, uptime, or health.

## How to Check
1. Run `uptime` to check system uptime and load average
2. Run `free -h` to check memory usage
3. Run `df -h` to check disk usage
4. Run `curl -s http://localhost:PORT/health` for application health endpoints

## What to Report
- Uptime duration
- CPU load (warn if >4.0 on a 4-core machine)
- Memory usage (warn if >80%)
- Disk usage (warn if >85%)
- Application status (up/down, response time)

## Thresholds
- Memory >80%: ⚠️ warning
- Memory >90%: 🔴 critical
- Disk >85%: ⚠️ warning
- Disk >95%: 🔴 critical
- Load >cores: ⚠️ warning
```bash

The agent reads this skill when it needs to check server health and follows the instructions. Skills are simple markdown files — no special syntax, no framework API, just clear instructions.


## Step 8: Enable Scheduling

A real agent doesn't wait to be asked. It does useful work on its own schedule.

Add cron jobs to your OpenClaw configuration:

```json
{
  "crons": {
    "morning-check": {
      "schedule": "0 7 * * *",
      "task": "Check server health. If anything looks concerning, alert me on Telegram.",
      "model": "anthropic/claude-haiku"
    },
    "daily-summary": {
      "schedule": "0 21 * * *",
      "task": "Summarize what we accomplished today. List open items for tomorrow.",
      "model": "anthropic/claude-haiku"
    },
    "git-backup": {
      "schedule": "0 3 * * *",
      "task": "Commit and push any uncommitted changes in the workspace.",
      "model": "anthropic/claude-haiku"
    }
  }
}
```bash

**Pro tip:** Use the cheapest model (Haiku) for scheduled tasks. They're usually simple, repetitive operations that don't need the reasoning power of Sonnet or Opus.

Restart the gateway to activate the cron jobs:

```bash
openclaw gateway restart
```bash

Your agent now runs tasks automatically at 7 AM, 9 PM, and 3 AM without any human prompting.


## Step 9: Add Sub-Agent Capability

For complex tasks, your agent can spawn sub-agents — independent sessions that handle specific work in parallel.

This is already built into OpenClaw. When your agent encounters a task that would benefit from delegation, it can spawn a sub-agent with a specific mandate:

- "Write a 2000-word article about X" → content sub-agent
- "Review this pull request and report findings" → code review sub-agent
- "Research competitors and compile a report" → research sub-agent

Sub-agents run in isolated contexts, complete their task, and report back to the parent agent. This is how you scale from a single assistant to a team of specialized workers.

To enable this, ensure your agent's `SOUL.md` includes permission to spawn sub-agents:

```markdown
## Delegation
You can spawn sub-agents for tasks that take more than 5 minutes.
Each sub-agent gets a specific task and reports back when done.
Use sub-agents for: writing, research, code review, data analysis.
Do the work yourself for: quick answers, simple tasks, anything under 2 minutes.
```bash


## Step 10: Test and Iterate

Your agent is now running. Here's how to verify everything works:

### Basic Conversation Test

Message your agent on Telegram: "What can you do?"

It should respond based on its SOUL.md identity and capabilities.

### Memory Test

1. Tell your agent: "Remember that my preferred meeting time is 2 PM"
2. Close the conversation
3. Start a new conversation and ask: "When do I prefer to have meetings?"

If memory is working, it remembers. If not, check that the memory files are being written and read correctly.

### Scheduling Test

Wait for a scheduled task to fire (or set a test cron to run in 2 minutes). Check that:
- The task executes
- Results appear in the right channel
- Memory files are updated

### Tool Access Test

Ask your agent to: "Check the disk usage on this server."

It should execute `df -h`, parse the output, and report back in a readable format.


## Going Further

### Add More Channels

Connect Discord, WhatsApp, or Slack so your agent is available wherever you communicate. Each channel is independent but shares the same agent memory.

### Build Domain-Specific Skills

The generic setup above works for a personal assistant. To build a specialized agent, write skills for your specific domain:

- **Customer support agent** — skills for reading tickets, searching knowledge bases, drafting responses
- **DevOps agent** — skills for monitoring, deploying, incident response
- **Research agent** — skills for web research, data analysis, report generation
- **Sales agent** — skills for lead tracking, email sequences, CRM updates

### Optimize Costs

Track your API usage. Most agent interactions don't need the most expensive model:

- **Simple tasks** (reminders, status checks, basic queries) → Haiku ($0.25/$1.25 per million tokens)
- **Standard tasks** (drafting, research, analysis) → Sonnet ($3/$15 per million tokens)
- **Complex reasoning** (strategic planning, code architecture, critical decisions) → Opus ($15/$75 per million tokens)

Configure your agent to use the appropriate model for each task type. This alone can cut your costs by 60-70%.

### Production Hardening

For a production agent that runs your business operations:

- **Set up monitoring** — alert if the gateway goes down
- **Configure backups** — daily git commits of the workspace
- **Add safety rails** — define what the agent can and can't do in SOUL.md
- **Implement logging** — write daily memory files so you can audit what happened
- **Set resource limits** — cap concurrent sub-agents based on your server's capacity


## The MrDelegate Option

Everything in this tutorial is what we do at MrDelegate — but packaged as a managed service.

If you followed this tutorial and got a working agent: great. You're in a small minority of people who can self-host and self-manage AI agents. Keep going. The DIY path gives you maximum flexibility and control.

If you got halfway through and thought "I want the result but not the maintenance" — that's exactly why MrDelegate exists. We handle the server, the configuration, the updates, the monitoring. You get a working agent on day one.

Plans start at $29/month at [mrdelegate.ai](https://mrdelegate.ai).


## Common Mistakes to Avoid

**Mistake 1: Making the agent too powerful too fast.** Start with basic capabilities. Add tools and permissions gradually as you understand how the agent behaves. An agent with unrestricted shell access on day one is a liability.

**Mistake 2: Not testing memory.** The memory system is the backbone of a good agent. Test it thoroughly. Break it on purpose. Fix it. If memory doesn't work, nothing else matters.

**Mistake 3: Ignoring costs.** API costs can spike if you're not careful. Use cheap models for background tasks. Set spending alerts on your API provider. Monitor usage weekly.

**Mistake 4: Overcomplicating the identity.** A 5000-word SOUL.md doesn't make a better agent. Start with 20 lines. Add specifics as you discover what behaviors you want to encourage or restrict.

**Mistake 5: Not reading the logs.** Your agent will make mistakes. The daily memory files and gateway logs tell you what happened and why. Read them. Learn from them. Improve the configuration.


## What's Next

You've built an AI agent. It runs on your server, remembers conversations, executes tasks, and works on a schedule. That puts you ahead of 99% of people talking about AI agents.

The gap between "I have an agent" and "my agent runs my operations" is iteration. Every day you use it, you'll discover something it does poorly. Fix it. Every week, add a new skill or capability. Every month, review what's working and what isn't.

The agents I run today are radically different from the first one I built. The architecture is the same — but the skills, the memory, the guardrails, and the workflows have been refined through hundreds of iterations.

Start simple. Ship it. Improve it daily. That's how you build an AI agent that actually matters.

## Ready to get started?

[Start your free trial](https://mrdelegate.ai/start) and experience the power of OpenClaw, hosted by MrDelegate.
Managed OpenClaw Hosting

Your AI assistant is ready.

Dedicated VPS. Auto updates. 24/7 monitoring. Live in 60 seconds. No terminal required.

Get Started → From $47/mo · No commitment · Cancel anytime