3 min read

Claude Code as a personal agent

Running a personal agent on plain Claude Code.

There are many options for building a personal agent, the most common being Hermes and OpenClaw, and, more recently, managed agents from frontier labs.

Early this year Anthropic was not permitting the use of a Claude subscription in third-party harnesses, and I wanted to see how far I could take just Claude Code as the harness for the agent, while keeping things simple. It went surprisingly well and I kept the setup. It has been maturing since.

The agent normally needs five things: staying alive, connections (Telegram, Discord, etc.), scheduling, memory, and a way to manage the context.

In this post I describe how this is achieved using standard tools, and how to get something simple running fast. But the harness allows us to go beyond a simple setup, and there will be more posts on that.

Staying alive

The agent is a normal Claude Code session, the same one you would start in a terminal. We wrap it using the standard expect tool that gives it the terminal it expects, and keep it alive using the native service manager, depending on the OS. On a Mac that is launchd, on Linux a systemd service.

Every session has an id. The start script creates the session once and resumes the same one every time after that. A crash, a restart or a reboot brings back the same conversation.

Connections

Claude Code Channels allow connecting messaging apps to Claude, or technically any other source we want. I used them to implement my own cross-agent messaging before Anthropic rolled it out natively. Official Channels exist for Telegram, Discord and iMessage. Follow the instructions in the docs.

Memory

Claude Code’s built-in auto-memory will be enough for most uses.

If you want something more advanced, you can consider things like Hindsight or Mem0. I have built my own memory system, which is over-engineered on purpose.

Context

Your agent’s context will eventually fill up. Claude Code handles this by running compaction; we just need to make sure the agent is designed so that nothing important is lost when it happens.

A PreCompact hook fires whenever a compaction is about to happen. We can use it to tell the session to save whatever it needs in memory and to schedule its tasks again. Then the session compacts and just continues.

Scheduling

Claude Code supports multiple ways of scheduling things, and the agent can arm the tasks itself. I keep my tasks in tasks.md, and the agent re-arms them when it starts. Scheduled tasks expire after 30 days, but the pre-compaction procedure triggers re-arming. This assumes the agent is used enough to trigger a compaction within that time.

Setup

The easiest way to set this up is to just prompt Claude. Use Opus for both the builder and the agent. The setup assumes the agent runs on a Mac and uses Telegram to communicate, but Claude will be able to adjust it for other cases.

Optimizing Claude Code

There are some further optimizations to make it even better:

  • Claude Code’s default prompt is large and not necessarily what we want for our personal agent. We can take the default system prompt (it can be found here) and trim it to only keep the essentials, such as tool-calling instructions, guardrails and whatever else you decide to keep.
  • A UserPromptSubmit hook runs when a task fires, before the model sees it. It allows us to programmatically check whether there have been any updates since the last run (e.g., whether there are new emails for the email triage task) and skip the turn if there is nothing to act on. This makes most calls cost zero tokens.