Build Open-Source AI Agents with Pi and pi-gateway

By LatestLLM Editorial ·

Build Open-Source AI Agents with Pi and pi-gateway

Learn how to create always-on AI assistants like OpenAI's Dots using Pi, pi-gateway, and Telegram, hosted on your own VPS with custom tool integration.

Key takeaways

  • You can build open-source 'dots' using the Pi framework and pi-gateway on an always-on machine like a VPS.
  • Agents are organized as directories, where an 'agents.md' file defines the system prompt and context.
  • Real-world capabilities are added via skills, such as the Google Workspace CLI for email/calendar and Firecrawl for research.
  • The pi-gateway uses a local SQLite database to route Telegram messages to specific Pi sessions based on User IDs.
  • The system supports multiple specialized agents simultaneously using different models via OpenRouter, such as Kimi K3 and Space Bunny Alpha.

Imagine having a fleet of specialized AI assistants living in your pocket—one that manages your calendar and emails, and another that conducts deep web research—all running on hardware you own and control.

⚡ TL;DR: You can build open-source alternatives to OpenAI's "dots" using the Pi agent framework and pi-gateway. By hosting these on a VPS and connecting them via Telegram, you create always-on agents with persistent memory via SQLite and real-world capabilities through tools like the Google Workspace CLI and Firecrawl.

The Simple Version

Most AI bots are like vending machines: you put in a prompt, and you get a response. But "always-on" agents are more like personal employees. They have their own desk (a workspace), their own set of tools (skills), and a way to contact you whenever they finish a task.

To make this work, you need a gateway. Think of the gateway as a digital switchboard operator. When you send a message on Telegram, the operator looks at who you are, decides which "employee" (agent) should handle the request, and routes the message to that agent's specific desk on your server.

How It Actually Works

Agent Workspaces as Directories

In this system, an agent isn't just a prompt; it is a physical directory on your server. Each agent has its own folder containing an agents.md file, which serves as the system prompt and provides the agent's identity and goals.

As the speaker explains, "Each agent is essentially its own directory with its specific tools, specific skills, and specific agents.md file." This means you can have a "Research Agent" folder and a "Personal Assistant" folder, each with entirely different personalities and capabilities.

🎯 Think of it this way: Instead of one giant brain trying to do everything, you have a filing cabinet. Each drawer is a different expert. When you want a researcher, you open the 'Research' drawer, and the AI only sees the tools and instructions inside that specific drawer.

Adding "Skills" via CLI

Agents become useful when they can interact with the real world. This is done through skills. Using a command like npx skills add, you can attach specific capabilities to an agent's workspace.

In the demo, the personal assistant agent is given access to the Google Workspace CLI. This allows the agent to read Gmail inboxes, summarize emails, and create calendar events. For the research agent, Firecrawl is used to enable web scraping and searching, allowing the AI to gather live data from the internet.

The Telegram Gateway

To avoid having to use a terminal or SSH every time you want to talk to your AI, the pi-gateway acts as the bridge. It runs as a background process on an always-on machine, such as a VPS (Virtual Private Server) or an NVIDIA DGX Spark workstation.

The gateway listens for incoming Telegram messages. When a message arrives, it uses a local SQLite database to route the conversation. It checks the source (Telegram), the agent name, and the session ID to ensure the message goes to the correct directory and maintains the conversation history.

Technical Deep Dive

The architecture described is a implementation of the Agentic Workflow pattern, where the LLM is decoupled from the interface and coupled instead to a filesystem-based context. This is inspired by projects like Hermes and OpenClaw.

Session Routing and State Management

The use of SQLite for session routing solves a major problem in bot development: statefulness. Because LLMs are stateless, the gateway must map a unique Telegram User ID to a specific Pi session ID. When a user sends a message, the gateway performs a lookup in SQLite; if no session exists, it initializes a new one in the corresponding agent directory.

This allows the user to trigger a /new command to wipe the current session and start fresh, which the gateway handles by creating a new entry in the database and a corresponding new session in the Pi harness.

The Harness: Pi and Tau

The core engine here is Pi, with Tau acting as a Python port of the Pi harness. These tools manage the loop of Thought → Action → Observation. When an agent decides to use a skill (like searching Gmail), the harness executes the CLI command, captures the output, and feeds it back into the LLM as a new observation.

For the models, the system utilizes OpenRouter to access high-performance models. The demo specifically uses Space Bunny Alpha (a stealth model) for the personal assistant and Kimi K3 for the research agent. This flexibility allows the user to swap models based on the task—using a faster, cheaper model for scheduling and a more reasoning-heavy model for research.

Filesystem-Based Context

By tying the agent to a directory, the system implements a primitive form of RAG (Retrieval-Augmented Generation) and long-term memory. Any files the agent writes to its workspace remain there. If the agent performs research and saves a summary to a text file in its folder, that file becomes part of its persistent environment, which it can reference in future sessions.

Why This Matters

This approach moves us away from the "Chatbot" paradigm and toward "Digital Agency." By hosting the gateway and agents on your own VPS, you maintain ownership of your data and your session history, rather than relying on a proprietary cloud provider's memory system.

Furthermore, the modularity of the pi-gateway means it can be extended. While the demo focuses on Telegram, the same logic can be applied to WhatsApp, Slack, or Email. This creates a unified AI interface that follows you across platforms while remaining anchored to a secure, private server.

Ultimately, this setup mimics the "dots" experience—specialized, always-on, and tool-capable—but does so using an open-source stack that developers can audit, modify, and scale according to their own needs.

Frequently asked questions

What is a VPS and why do I need one for this?

A VPS (Virtual Private Server) is a virtual machine hosted in the cloud that stays powered on 24/7. You need it so your AI gateway can constantly listen for Telegram messages and respond even when your personal computer is turned off.

Can I use my own local models instead of OpenRouter?

Yes, the speaker mentions that you can run these models locally if you have the hardware, such as an NVIDIA DGX Spark workstation, instead of using API keys from OpenRouter.

How does the agent actually 'do' things like send emails?

The agent uses 'skills,' which are essentially wrappers around CLI tools. For example, it uses the Google Workspace CLI to execute commands that interact with Gmail and Calendar APIs.

Sources

Source: “Build Open-Source AI Agents with Pi and pi-gateway” on YouTube by HuggingFace

Tags: AI Agents, Open Source, LLM, Telegram Bot, Self-Hosting

Related articles