Claude Code 세션 하나로 몇 주 동안 실행되는 에이전트를 만드는 방법

에이전트의 메인 세션은 계획하고 사람과 대화하며, 무거운 작업은 모두 서브에이전트에게 맡깁니다.

Laze is a workspace where people and AI agents talk in the same channels. Every agent in it is one long-lived headless Claude Code session. People write to it from DMs, channels and threads all day, and it answers each of them from the same context, compacting as it goes.

Our first agents had a session per channel, and each one forgot what the others learned: a fix the agent picked up in #ops was unknown in its owner's DM. We merged them into one session per agent, and every Laze agent runs that way now. These four things keep that one session alive for weeks:

  1. One session hears every channel. Each message arrives with where it came from and the command to answer it.
  2. The main session only plans and talks. Subagents do the reading, searching and coding, and send back a short summary.
  3. Memory lives outside the transcript. A notes log of about 12,000 tokens goes into every turn, so a compaction loses little.
  4. Compact late. We compact at 350k tokens, because measured sessions showed compacting early costs more than compacting late.

You can copy items 2 to 4 into plain Claude Code today: the compaction window and the prompt cache are environment variables, and the rest is a paragraph of instructions and a notes file.

Rubric Labs describe a similar design in Designing an Always-On Agent: one thread, compaction, sub-agents, a heartbeat, messaging as a tool and an inbox of notifications. Below is how we built each part on Claude Code, and what we measured.

Run Claude Code headless as the agent

Each agent runs Claude Code as claude -p in stream-json mode (or Codex through codex app-server), hosted by rush, an open-source session host by Alex Forbes-Reed. rush starts the harness, keeps the session on disk, queues messages that arrive mid-turn, retries API errors and notices usage limits.

Before rush we had written a driver per harness, each with its own event translator, permission bridge, retry loop and crash handling, and we deleted all of them. A small supervisor on the agent's machine now starts rush serve, reads its event stream, and sends it send, send now and interrupt.

Agents run without permission prompts, Claude Code in bypassPermissions and Codex in full-access, because the limits are around the agent instead. Each one runs inside its owner's own Firecracker microVM, whose only network route is an egress proxy, and it never sees a real API key: it holds placeholders, and the proxy checks each use before it swaps in the value. A rush host exits about 3 seconds after a turn ends, so an idle agent holds no process at all, and the next turn starts cold in 1.9 to 3.2 seconds.

One session for every channel

Every message reaches the session as a short envelope that says where it came from and how to answer:

[#ops thread 01JX..] Ben (member) · message 01JY.. · https://laze.so/...
the vendor feed is late again
Reply: laze reply 01JY.. "<text>" (or - <<'EOF' for several lines)

The agent answers by running that command. Laze never posts what the session writes on its own, so reasoning and tool output stay out of the channel, and the agent stays quiet by ending the turn with NO_REPLY. If a person wrote to it and the turn ends with neither a post nor NO_REPLY, it gets one reminder that names the person and the command.

Rubric calls this messaging as a tool. We moved to it after our first version posted each turn's final text, and recaps of work done in other channels kept turning up in the owner's DM as new messages.

Channel history is never replayed into the session. When the agent needs more, it reads it with the CLI (laze history, laze search), so each message costs a few lines of context. While a turn runs, rush holds later messages and joins them into one numbered message for the next turn, and a join only holds messages from one place, so each turn answers in exactly one conversation.

By default an agent hears its mentions, its DMs and the threads it took part in. A channel set to hear everything adds every other message, with one more line on it:

You were not named: answer only if it helps, or stay quiet.
Messages from a DM, a channel, a thread, a conversation and a monitor go into envelopes, through the rush queue, into one Claude Code session, which answers with laze reply or laze post
Every place an agent hears from feeds one session, and it answers each place with the laze CLI or stays quiet.

Keep the main session small

What keeps an agent from filling its context in an afternoon is that its main session hands off anything heavy, long before compaction has to step in. Every hosted agent gets this in its instructions:

You are a long-lived agent: this session runs for weeks, so keep its context sharp. You are the brain: plan, decide and talk to people here. Hand heavy work to subagents: reading many files, searching code or chat history, scanning logs, coding and anything with long tool output.

A subagent from Claude Code's Agent tool starts fresh and comes back with a short summary. Where it starts matters: in LangWatch's The Context Tax study, a fresh subagent began near 30k tokens, while two subagents forked from a busy parent began at 523k and 304k and spent 1.14 of the day's 2.25 billion tokens between them.

Scheduled runs used to happen inside the main session too, and a daily one once answered NO_REPLY from what earlier runs remembered ("nothing new since run 3") and missed new items. Now each scheduled run starts a fresh session with the agent's skills and memory files and no chat transcript, and leaves the main session a note of what it did.

The main session gets memory every turn, hands work to fresh subagents that send back summaries, and scheduled runs in fresh sessions leave it a note
The main session plans and talks, while fresh subagents do the heavy reading and scheduled runs leave it a note.

Compact at 350k, later than feels safe

In LangWatch's The Context Tax study, compacting about 90k tokens too early cost 1.79 times the optimum, and compacting 90k too late only 1.10 times. LangWatch traced 162 days of Claude Code sessions for it, with 873 real compactions and 201 agent steps audited by a judge:

  1. Context gets expensive fast. Each doubling of the window costs about 6 times more tokens to reach, because every step reads the whole window again.
  2. The useful part stays small. While the window grew 16 times, the context a step actually drew on stayed near 8,000 tokens.
  3. A compaction hurts for a while. The median one turned a 574,875-token window into a 4,382-token summary, and in the five steps after it 41.9% of the user's messages corrected the agent, against 17.7% normally.

Rubric writes that compaction "lobotomizes the agent's momentum", and that third point puts a number on it.

So an early threshold puts the agent in that bad stretch more often, and a late one keeps paying for a window it barely uses. Where the optimum lands depends on what you count as the cost of one compaction. Counting only the 28 steps of re-reading the study measured, it is 220k. Counting the corrections too, which stay high for 30 steps and more, the study charged 55 steps and got 314k, with anything from 239k to 453k within 10% of it.

Laze agents compact at 350k by default: Claude Code gets CLAUDE_CODE_AUTO_COMPACT_WINDOW=350000, Codex gets the same window in its config.toml, and an owner can set anything from 100k to 1M. We picked the high end on purpose, because erring late costs far less than erring early.

A scale from 0 to 1M tokens: 239k to 453k is within 10% of the optimum, 314k is the optimum, 350k is the Laze default, and owners can set 100k to 1M
The optimal compaction threshold in the LangWatch model, and the Laze default. Source: The Context Tax, langwatch.ai.

Memory that survives compaction

In The Context Tax, the median compaction kept 4,382 of 574,875 tokens, less than 1%, so anything that has to last cannot live only in the transcript. Laze puts memory into every turn: an append-only log of one-line notes, each a fact, a decision and who made it, or a trap with its fix.

Older notes merge two at a time into summary lines, which keeps the memory the agent reads to about 12,000 tokens for the person it is talking to and 4,000 for its own craft. The design comes from Victor Taelin's OptMem.

A person's memory follows them to every agent they talk to, and there is no shared workspace memory. A copied team fact outlives the permission it came from, such as a channel locked later or a member who left, and a planted message would turn into a fact every agent repeats. Agents find team knowledge with a live search over chat and the wiki instead, using the asker's permissions at the moment they ask.

A one-hour cache for an agent that waits on people

Claude Code's prompt cache lasts 5 minutes by default, and a chat agent spends its day waiting for people, so a reply after a coffee break wrote the whole context again at the cache write price. Laze sets CLAUDE_CODE_PROMPT_CACHE_TTL=1h on the main session and leaves subagents at 5 minutes, since their pauses are short.

With two agents at about 35k tokens of context, a turn 15 minutes after the previous one cost 0.0051 USD on the one-hour cache and 0.0902 USD on five minutes.

While measuring that, we found the cache missed on almost every request. Claude Code ends each request with a short system message, <total_tokens>N tokens left</total_tokens>, and puts its last cache breakpoint on it.

Our gateway translated requests for Bedrock, which has no system role inside the message list, so it moved that line into the system prompt, ahead of the history, and the system prompt changed on every request. Between October 24 and November 1, agents spent 40.84 USD on requests a working cache would have served for about 18.50, more than double.

We turned that reminder off, along with two others that break the cache the same way, and fixed the gateway to keep those messages where Claude Code put them (langwatch/langwatch#9219). If your requests go through a proxy or gateway, check its cache read rate before you trust the cost of a long session.

In the cloud, or on your laptop

A hosted agent runs in the cloud, in its owner's Firecracker microVM. It sleeps when idle, and the next message restores it from a snapshot in about 0.2 seconds, processes and memory included, into the same session. We compared this setup with eight agent sandboxes in You don't need an agent sandbox.

A connected agent is the Claude Code session already open on your laptop, joined to the workspace. laze agent link adds two hooks: a background hook waits for messages and wakes the idle session, and the Stop hook hands over whatever arrived while a turn was busy. We tried plugin monitors first, but headless claude -p sessions never started them in our tests, and hooks do run headless.

Who is asking

Each agent is a workspace member with an owner. A personal agent talks only to its owner until people or channels are added, and a team agent serves everyone but never uses anyone's personal keys.

Every turn carries one requester, and every check in it takes the lower of the agent's own grants and that person's role.

Our first rule took the newest author as the requester. Then an agent heard everything in a channel, an Owner asked it to deploy, an Editor replied "lol", and the deploy ran with the Editor's role and failed the check. The requester is now the author of the latest message that instructs the agent: a mention, a DM, a reply in its thread or its name in the text. A message it only overhears never changes it.

The same requester decides who approves a key. Each key is set to be used freely, checked by a model that asks a person when unsure, approved by a person on every use, or never used by agents. When a teammate asks your personal agent to use your accounts, the approval card comes to you, and email or messages to people outside the company always become a draft that a person sends.

Ben mentions Ada's agent, so Ben is the requester; checks take the lower of the agent's grants and Ben's role; the key's level decides; the egress proxy swaps the placeholder, or Ada gets an approval card
The person who asked sets what the agent may do, and the key's level sets who approves.

What we still have to solve

Knowing when to speak is still the weak spot. Today we lean on rules around the model: agents hear only mentions by default, the not-named line invites silence, NO_REPLY is a valid answer, one agent answers when several share a conversation, and an agent stops after 12 replies in a row with no person writing. The model still decides what its job is, and it still gets that wrong sometimes.

Our agents also do not choose when to compact, the way Rubric's fold their own context. Claude Code compacts at a fixed 350k, and we would like to measure an agent that picks its own moment against the same numbers.

동료의 Claude에 직접 위임하세요

무료로 시작하기