文档Docs
心跳——Agent 自己醒来看一眼Heartbeat — the Agent checks in by itself
空闲一段时间后,Agent J 会叫醒你的主 Agent,看看后台任务和你的待办;只有需要你的时候才出声。多久看一次、看什么,一句话就能改。After a quiet spell Agent J wakes your main Agent to look at background work and your to-dos; it only speaks up when something needs you. Change how often and what it checks in one sentence.
空闲满一段时间(默认 60 分钟),Agent J 主机先检查本机记录;没有发现要处理的事,就只记一行日志,不调用模型。有事才叫醒主 Agent,并告诉它为什么醒来。只有需要你知道或决定的事才发到手机上。
Claude Code、Codex、OpenCode 都一样,共享模式和独立模式都有。心跳由你Agent J 主机上的 Agent J 发出,不靠云端,也不靠某个 AI 工具自带的功能,所以Agent J 主机重启、程序崩溃以后,中途挂掉的活也能接着干。
心跳时会发生什么
- 空闲满了设定的时间:你没发消息,Agent 没在干活,Agent J 主机前也没人打字。
- 主机先检查到期待办、日历、完成报告、待处理分身消息等本机记录;没有发现要处理的事就跳过,不调用模型。
- 发现事情且通过费用保护后,Agent J 才发送以
〔Agent J 心跳〕开头的消息,带上唤醒原因和提示的短哈希。 - Agent 按心跳提示去看:后台的子 Agent、headless 任务和定时任务,最新的运行报告,你的待办和它答应过的事,中途挂掉的活。
- 一切正常就不出声(手机上没有新页面,也没有提醒);有事要告诉你,手机上会收到一条简短消息,标着心跳检查。
改用系统定时的 Agent J 定时任务,报告放在 Agent J 的状态目录里,开着隔离时 Agent 看不到;每次运行的结果直接发到你手机上,见已有的定时任务改用系统定时。
心跳不会打断你:Agent 正在干活、你的消息在排队、有审批卡片没处理、正在压缩、已经全部停下,或者在夜间升级时段(03:00–05:00),它都会等。你和 Agent 一聊天,下一次心跳就自动往后推。
每天固定时间唤醒
除了空闲满了检查,还可以每天到点检查一次,比如每天早上 7:30;同样只有发现要处理的事才叫醒 Agent。时间按你Agent J 主机上的本地时间算。
到点时如果 Agent 正忙、或者在夜间升级时段,它照样会等;还要通过本机预检、安静时段和费用保护,不保证当天调用模型。Agent J 主机在那个时间关着、开机时已经过了一个多小时,这一天就跳过,第二天照常。Agent J 主机重启不会让同一天醒两次;到点前后刚好有一次空闲心跳的话,就算作当天这一次,不会连着醒两回。每天定时唤醒跟着心跳的总开关走:agentj heartbeat off 以后它也不再醒。
一句话就能改
跟 Agent 说一句,它会执行命令并回你「记下了」:
| 你说 | Agent 执行 |
|---|---|
| 「以后每 30 分钟看一次」 | agentj heartbeat set 30m(5 分钟到 24 小时:45m、2h、1d) |
| 「别再定时叫醒了」 | agentj heartbeat off(恢复用 on) |
| 「每天早上 7:30 叫醒你看一眼」 | agentj heartbeat daily 07:30(关掉用 agentj heartbeat daily off) |
| 「心跳的时候顺便盯着备份任务」 | 改心跳提示 |
| 「心跳现在怎么设置的」 | agentj heartbeat status |
手机上:设置 → 心跳,有同样的开关。「空闲多久后醒来」是一个下拉框,从 30 分钟到 24 小时;选「自定义…」可以自己填 5 到 1440 分钟。打开「每天固定时间唤醒」再选几点,就多一次每天定时的心跳。改好马上生效。
夜间和长对话保护
默认按 Agent J 主机本地时间 23:00–07:00 安静运行:提示变更等非紧急原因会延后;到期待办、日程、完成报告和待处理分身消息仍可叫醒 Agent。打开设置 → 心跳可修改或关闭安静时段。无事不调用模型的预检全天有效。
上下文超过保护阈值(默认 100,000 token),或识别为高价模型时,自动心跳先暂停,手机提示你按原有交接流程压缩(/compact)。本次会话成功压缩后,解除首次高价模型保护;实测上下文仍超过阈值时,自动心跳继续暂停。不会自动清空对话。可请 Agent 修改 heartbeat.context_max_tokens 调整阈值。
心跳提示
Agent 醒来做什么,写在你Agent J 主机上的一份短文档里:
~/.config/agentj/heartbeat.md
Agent J 第一次启动时会写好一份默认的。你或 Agent 都可以改(agentj heartbeat prompt show、agentj heartbeat prompt edit、agentj heartbeat prompt reset)。没改过的提示,Agent 不用每次重读:每次心跳都带着它的哈希,只有改过、Agent J 主机重启或者压缩以后,才会附上全文。不要在里面写密码或 key——看起来像密钥的内容 Agent J 会拒收。
额度管家
我正在聊的 AI 额度用完了
用 ChatGPT 账号登录 Agent J 主机上的 Codex,再在手机发送切换命令(/switch-harness),选择 Codex,就能换掉正在聊天的 AI。
这是切换当前主对话的操作,不会把 Claude 的额度转给 ChatGPT,也不是打开 ChatGPT 网页聊天。中国大陆用户也可以改用 OpenCode 连接火山方舟、阿里百炼或 Kimi;先按AI 工具与模型配置服务商,再用切换命令(/switch-harness)选择 OpenCode。
额度用完了,到点自动接着做。 对话或工作流因为 AI 账号额度用完而停下时,Agent J 会记下它,等这个 AI 自己报的重置时间一到,自动发一句「继续」把没做完的做完——只续一次。手机聊天页上方会出现一条横条「额度用完,预计 HH:MM 自动继续」,点 ✕ 就取消。到点前你自己发了消息、按了全部停下,或者 Agent 正忙,这句「继续」就不发了。我们正在说话的主对话不会被切走,但你自己换了 AI(/switch-harness)之后它会立刻接着做。AI 没给重置时间、只是瞬时限流、或者是余额不足的,不会自动继续,手机上会说清楚。Agent 也能看和取消:agentj quota resume。
只有真用完才算「额度用完」。 瞬时限流、服务繁忙、上下文满了这类报错不算。AI 确认额度用完时以它为准:如果手机上的 5H / WEEKLY 读数看着还没满,那是旧读数,读数旁会标「读数可能过期」,新读数到了自动更新;读数用得久了,旁边会显示更新时间。Claude 额度用完时,提示里会提醒你去 Claude 设置 → Usage 看看有没有「Reset for free」(免费重置),Agent J 不会替你点。只是某个模型单独到了上限(比如 Opus 专属周限额),会说清是哪个模型,并建议换成哪个模型马上接着做。同一个 AI 的同一种上限,对话和工作流显示同一个重置时间。
后台任务自动转交
只管新起的定时任务 / 工作流,不动聊天线程。 Agent J 主机上登录了不止一个 AI(比如 Claude Code 和 Codex)时,当前这个的 5 小时额度到 80% 或周额度到 90%,新开始的定时任务 / 工作流会先跟另一个额度还宽裕的 AI 握一次手(发一句问候、要它真回话)。能用的话:默认在手机上问你是否改用另一个 AI 来做,你同意才切,不同意时,5 小时额度不到 90% 才由原来的 AI 接着干;在 设置 → 额度告警时自动转交(只管后台任务) 打开后就不问了,直接切,任务结果里写一句改用了谁。我们正在说话的这个主对话不会被切走(要换主对话的 AI 用 /switch-harness);工作流限定了可用的 AI 时,只在它允许的几个 AI 里换。
5 小时额度到 90% 或以上时,如果没有可用的其他 AI,或你拒绝转交,就不再用当前账号启动新的后台任务;等额度重置或改用其他 AI。80% 是告警和尝试转交的门槛,不是停止新任务的门槛;这项限制不会主动切换主对话。
已因额度用完停下的工作流另有自动续跑:打开这个开关、又有别的 AI 额度宽裕时,会在另一个 AI 上重新启动一次工作流。已经运行的任务不会被搬走;主对话只由你用切换命令(/switch-harness)切换。
订阅制的 AI 工具通常按 7 天给一份周额度,平均每天约 14.3%。只要查得到额度,Agent J 就有义务提醒你:心跳时它会按固定规则看一眼各个 AI 工具的周额度和 5 小时额度,有该说的就告诉你,并结合你的工作流和待办建议 1–3 件可以多派的大活。
| 情况 | Agent 会说 |
|---|---|
| 用得比均分线慢了一天以上 | 额度会浪费,可以多派点活 |
| 到了后半周(只剩 3 天以内),剩得还多 | 主动建议派大活 |
| 登记过的额外 reset 7 天内过期 | 尽早提醒;这周加大工作量,额度一见底就用掉它 |
| 额度见底,又还有 reset | 提醒你现在就按 reset |
| 周额度用到 90% 以上,又没有 reset | 提醒你;别的 AI 账号还有余量就点名 |
| 5 小时额度用到 80% | 开始预警;别的 AI 账号还有余量,就建议新活交给它 |
同一类提醒每天最多一次(5 小时预警每个 5 小时窗口一次),晨报里也会带上。能查到什么:Claude Code 读它自己给状态栏的额度数据(需要 agentj config claude-statusline on),Codex 读它自己会话记录里的额度数据,OpenCode 取决于背后的服务商,多半查不到。查不到的就标查不到,不猜。各家的额外 reset 都不在本地,你告诉 Agent 一声(我还有一次 reset,20 号过期),它会用 agentj quota reset add claude --expires 2026-10-20 记下来。
自己看:agentj quota。不想听额度提醒:跟 Agent 说别提醒额度了(agentj quota off,恢复用 on)。
费用
无事跳过的预检不调用模型。真正叫醒 Agent 才会使用模型额度;长对话可能花得更多。点手机顶栏的用量,查看累计心跳用量。数字来自 AI 工具回报;工具未提供用量时显示未知,不当作零。
AI 工具自己也有定时器的话
Claude Code 可以自己安排唤醒(比如 /loop)。两者可以同时用:Agent J 的心跳是兜底,任何 AI 工具都有,工具关掉再打开以后照样会来。Claude Code 自己的唤醒在任何档位都不能请求审批,要你批准的活交给心跳或还在 Agent J 里运行的定时任务。
后台活结束就汇报
后台活结束时,Agent J 会主动叫醒主 Agent,默认开启。主 Agent 正忙就等这一轮结束;一分钟内接连结束的活合并汇报。Agent 先读实际报告与证据,再用一两句话告诉你结果,报告缺失也会说明并追查。主 Agent 当场等着结果的子 Agent(前台子 Agent)结束时不会再叫醒它,因为结果已经在这一轮里了;只有放到后台去的子 Agent 和命令结束才会。你自己那一轮派出去的后台活,结束后醒来的那一轮按你签过的审批档位处理;用 agentj busy run 或 agentj busy start 启动的、Agent J 没看到是你派出去的,结束时按来源不明处理:照样读报告、告诉你结果,但不能请求审批。见长程任务。
这条完成提示不依赖心跳,agentj heartbeat off 不会关闭它。要关闭,告诉 Agent 后台活结束不用叫醒它,它执行 agentj config set background_done.enabled false;恢复用 true。Claude Code、Codex 和 OpenCode 都适用。
派活前选择有新鲜用量的工具
主 Agent 新派后台工作前运行 agentj quota --pick --json。只有 Claude Code 和 Codex 的本周、五小时用量都可核实且不超过五分钟前,才给推荐;缺一项、过期或重置时间不可靠就不推荐,不把未知当成零。五小时达到 80% 或本周已用完的账号不参与推荐,再比较本周使用速度和余量,并解释原因。
这只是派活参考,不自动切换当前聊天,不覆盖你指定的工具,也不放宽已有 80%/90% 派活限制。还要确认所选工具已登录并能正常连接。
编辑巡检清单
打开设置 → 心跳 → 查看或编辑心跳提示,在全屏编辑器里查看和修改。四角分别是取消、恢复默认、预览和保存;恢复默认先改草稿,保存后生效。取消保留原文,保存失败会保留草稿。设置里的文字字段和通讯录备注也用同一编辑器。
出厂清单检查本机所有定时任务(Linux systemd timer/cron、macOS launchd/cron、Windows 计划任务)自上次心跳以来的日志和报告、Agent J 好友的异常或卡住的请求,以及邮件/微信/Telegram 分身有没有漏交、处理不理想或无人认领的事;主 Agent 需要时补回复或接手,仍遵守原有审批。忙时也按同一份清单顺手巡检,忙时不会触发心跳。
升级保留你原有的 heartbeat.md。doctor 和设置提示「出厂清单有更新,可一键合并」时,点合并出厂清单会保留你的文字再追加缺少的清单。
主 Agent 可运行 agentj inbox avatar-log --since 2h(也可写日期或时间,加 --json)列出本机保留的分身往来摘要、回复、处置结论和送达状态。复核时应覆盖上次心跳以来的时间;旧记录缺回复或送达证据时显示未知。记录有保留期限和大小上限,截短或通道不可用会明确提示,不能把没有记录当作一切正常。
After a quiet spell (default 60 minutes), the Agent J host checks local records first. If it finds nothing to handle, it writes one log entry without calling the model. When something needs attention, it wakes the main Agent with the reasons. Only information or decisions you need reach your phone.
This works the same with Claude Code, Codex and OpenCode, in shared and independent mode. It comes from Agent J on your computer, not from the cloud and not from a feature of one AI tool, so it also picks up work after a crash or a restart.
What happens at a heartbeat
- Nothing has happened for the interval: no message from you, no turn, no typing at the Agent J host.
- The host checks local records for due to-dos, calendar entries, completion reports and pending avatar messages. Nothing to handle → no model call.
- When something needs attention and cost protection allows it, Agent J sends a
〔Agent J 心跳〕("Agent J heartbeat") message with the wake-up reasons and the prompt’s short hash. - The Agent follows the heartbeat prompt: background sub-Agents, headless jobs and scheduled tasks, the newest run reports, your to-dos and anything it promised, work that died half-way.
- All fine → it stays silent (no page, no notification). Something to tell you → a short message on the phone, labelled Heartbeat check.
Agent J scheduled tasks moved to the system scheduler keep their reports in Agent J's state directory, which your Agent can't see with isolation on; each run's result goes straight to your phone. See Move existing scheduled tasks to the system scheduler.
A heartbeat never interrupts: it waits while the Agent works, while a message of yours is queued, while an approval card is open, during a compaction, while everything is stopped, and in the nightly upgrade window (03:00–05:00). Talking to the Agent simply moves the next heartbeat back.
A fixed time every day
Besides checking after a quiet spell, the host can check once a day at a set time — say 7:30 every morning. It still wakes the Agent only when something needs attention. The time is the Agent J host's local time.
If the Agent is busy at that moment, or it falls in the nightly upgrade window, the beat waits as usual. Local prechecks, quiet hours and cost protection still apply; a model call that day is not guaranteed. If the Agent J host was off then and Agent J started more than an hour after it, that day is
skipped and the next day works as normal. A restart never gives you two daily beats on one day, and an idle beat that
happens right at that time counts as the day's beat, so you never get two in a row. The daily time follows the main
switch: after agentj heartbeat off it stays quiet too.
Change it in one sentence
Tell the Agent, it runs the command and says "noted":
| You say | The Agent runs |
|---|---|
| "Check every 30 minutes from now on" | agentj heartbeat set 30m (5 minutes to 24 hours: 45m, 2h, 1d) |
| "Stop waking up on a timer" | agentj heartbeat off (on to resume) |
| "Wake me up every morning at 7:30" | agentj heartbeat daily 07:30 (agentj heartbeat daily off to stop) |
| "On each heartbeat also watch the backup job" | edits the heartbeat prompt |
| "How is the heartbeat set?" | agentj heartbeat status |
On the phone: Settings → Heartbeat has the same switch. "Wake up after being idle for" is a drop-down from 30 minutes to 24 hours; "Custom…" lets you type 5 to 1440 minutes. Turn on "Also wake up every day at" and pick a time for one extra beat a day. Changes apply at once.
Quiet hours and long conversations
Quiet hours default to 23:00–07:00 in the Agent J host’s local time. Non-urgent reasons such as a changed prompt wait; due to-dos, calendar entries, completion reports and pending avatar messages can still wake the Agent. Change or disable quiet hours in Settings → Heartbeat. Empty prechecks make no model call throughout the day.
Automatic heartbeats pause when context exceeds the protection threshold (100,000 tokens by default), or a recognised expensive model is selected. A phone notice asks you to follow the existing handoff and compaction flow (/compact). Successful compaction releases the initial expensive-model guard for this conversation; automatic heartbeats still pause when measured context exceeds the threshold. Agent J does not clear the conversation automatically. Ask the Agent to change heartbeat.context_max_tokens to adjust the threshold.
The heartbeat prompt
What the Agent does when it wakes is a short note on the Agent J host:
~/.config/agentj/heartbeat.md
Agent J writes a default on first start. You or the Agent can change it (agentj heartbeat prompt show,
agentj heartbeat prompt edit, agentj heartbeat prompt reset). The Agent does not re-read an unchanged note every
time: each heartbeat carries the note's hash, and the full text comes along only when it changed, after a restart and
after a compaction. Never put a password or key in it — Agent J refuses text that looks like one.
The quota steward
The AI I am chatting with has run out of quota
Sign in to Codex on the Agent J host with your ChatGPT account, then send the switch command (/switch-harness) on your phone and choose Codex to change the AI you are chatting with.
This changes the main conversation’s AI; it does not transfer Claude quota to ChatGPT or open the ChatGPT website. In mainland China, you can also use OpenCode with Volcengine Ark, Alibaba Bailian or Kimi: configure the provider using AI tools and models, then choose OpenCode with the switch command (/switch-harness).
Out of quota? It picks up again by itself. When a conversation or a workflow stops because the AI account's usage limit is used up, Agent J marks it and, once the reset time that AI reported has passed, sends one 「continue」 so the unfinished work gets done — once only. The chat shows a thin bar 「Quota used up · continues automatically at HH:MM」; tap ✕ to cancel. If you send a message yourself first, press Stop everything, or the Agent is busy, the 「continue」 is not sent. The conversation never switches AI automatically; after you switch it yourself (/switch-harness), it continues at once. No reset time given, a momentary rate limit or an empty balance: nothing continues by itself, and the phone says so. The Agent can list and cancel them too: agentj quota resume.
Only a real used-up limit counts as out of quota. A momentary rate limit, a busy service or a full context does not. When the AI confirms its limit is used up, that wins: if the phone's 5H / WEEKLY readings still look roomy they are old, so they are marked 「may be out of date」 until a fresh reading arrives, and an older reading shows when it was measured. When Claude's limit is used up, the notice also suggests checking Claude settings → Usage for 「Reset for free」; Agent J never presses it for you. When only one model is capped (an Opus-only weekly limit, say), the phone names that model and suggests the one to switch to so you can carry on now. The same limit of the same AI shows one reset time, in the chat and on every workflow.
Automatic handoff of background tasks
Only newly starting scheduled tasks / workflows are handed over; the chat thread stays put. With more than one AI signed in on the Agent J host (say Claude Code and Codex), once the current one's 5-hour quota reaches 80% or its weekly quota 90%, a newly starting scheduled task / workflow first does a handshake with another AI that still has room (a greeting it must really answer). If that works, by default your phone asks whether another AI should do this one and it only moves if you say yes — otherwise the same AI carries on only while its five-hour quota is below 90%. Turn on Settings → Auto-handoff on low quota (background tasks only) and it no longer asks: it moves, and the task's result says which AI ran it. The conversation you are having never moves (for that, /switch-harness); a workflow configured to use certain AI tools only moves among those tools.
At 90% or more of the five-hour quota, if no alternative is available or you decline the handoff, Agent J does not start a new background task on the current account. Wait for the quota to reset or use another AI. The 80% threshold starts warnings and handoff attempts; it does not itself stop new tasks. This rule does not switch your main conversation.
A workflow already stopped by a used-up quota has a separate automatic retry: with this switch on and another AI available, Agent J starts a new workflow run there once. Running jobs are not moved; only you switch the main conversation with the switch command (/switch-harness).
Subscription AI tools usually give a weekly quota over 7 days — about 14.3% a day on average. Whenever the quota can be read, Agent J has a duty to tell you: on a heartbeat it checks each AI tool's weekly and 5-hour quota by fixed rules, tells you what matters and, from your workflows and to-dos, suggests 1–3 big jobs you could hand over.
| Situation | The Agent says |
|---|---|
| More than a day behind the even pace | The quota will go to waste — give me more work |
| Second half of the week (3 days or less left) with plenty left | Proposes big jobs |
| An extra reset you recorded expires within 7 days | Says so early; more work this week, use the reset as soon as the quota runs out |
| Quota used up and a reset still available | Press the reset now |
| Weekly quota 90% or more used, no reset left | Warns you; names another AI account that still has room |
| The 5-hour window 80% used | Warns you; if another AI account has room, suggests new work goes there |
Each kind of reminder comes at most once a day (the 5-hour warning once per 5-hour window); the morning brief carries it too. What can be read: Claude Code's own status-line quota data (needs agentj config claude-statusline on), Codex's own session logs, and OpenCode depending on the provider behind it — usually nothing. What cannot be read is shown as unknown, never guessed. No tool keeps extra resets locally: tell the Agent (I have one reset left, it expires on the 20th) and it records it with agentj quota reset add claude --expires 2026-10-20.
See it yourself: agentj quota. No quota reminders: tell the Agent stop the quota reminders (agentj quota off, on to resume).
Cost
A precheck with nothing to handle makes no model call. An actual wake-up uses model quota, and a long conversation can cost more. Tap usage in the phone’s top bar to see accumulated heartbeat usage. Figures come from the AI tool; missing usage is shown as unknown, never zero.
If your AI tool has its own timer
Claude Code can schedule its own wake-ups (for example /loop). Both can run together: the Agent J heartbeat is the
safety net that works on every AI tool and still fires after the tool itself was closed and reopened. Claude Code's own
wake-ups can't ask for approval at any level; give work that needs your OK to the heartbeat or a scheduled task still run by Agent J.
Report as background work ends
Agent J wakes the main Agent when background work ends; this is on by default. A busy Agent waits until its current turn finishes. Jobs ending within the same minute are grouped into a report. The Agent reads the actual reports and evidence first, then tells you the result in one or two sentences; it also says when a report is missing and investigates. A sub-agent the main Agent waited for (a foreground sub-agent) does not wake it again when it ends, because its result is already in that turn; only sub-agents and commands sent to the background do. When background work sent off by your own turn ends, the turn that wakes up follows the approval level you signed. Work started with agentj busy run or agentj busy start, or that Agent J did not see your turn send off, comes back as unknown source: the Agent still reads the reports and tells you the result, but it can't ask for approvals. See Long tasks.
Completion notices work independently of the heartbeat: agentj heartbeat off leaves them on. Say “stop waking for completed background work” and the Agent runs agentj config set background_done.enabled false; use true to restore them. This works with Claude Code, Codex and OpenCode.
Choose from fresh usage before dispatch
Before new background work, the main Agent runs agentj quota --pick --json. It recommends only when both Claude Code and Codex have verifiable weekly and five-hour readings no older than five minutes. Missing, stale or unreliable reset data yields no recommendation, never an invented zero. Accounts at 80% of the five-hour limit or with the weekly limit exhausted are excluded; weekly pace and headroom explain the choice.
This is dispatch advice. It does not switch the current chat, replace your explicit choice or relax the existing 80%/90% dispatch limits. The selected tool must also be signed in and be able to connect normally.
Edit the watchlist
Open Settings → Heartbeat → View or edit heartbeat prompt. The shared full-screen editor offers Cancel, Restore defaults, Preview and Save. Restore changes the draft first; Save applies it. Cancel preserves the original, and a failed save keeps your draft. Settings text fields and contact notes use the same editor.
The factory watchlist checks all local scheduled-job logs and reports since the last heartbeat (Linux systemd timers/cron, macOS launchd/cron, Windows Task Scheduler), unusual or stuck Agent J friend requests, and mail/WeChat/Telegram avatar work that should have been handed over, was handled poorly or remains unclaimed. The main Agent follows up or takes over under existing approvals. It follows this same file while working too; heartbeats do not fire while busy.
Upgrades preserve your heartbeat.md. When doctor or Settings reports a factory checklist update, Merge factory checklist appends the missing checklist while keeping your text.
The main Agent can run agentj inbox avatar-log --since 2h (or an ISO date/time; add --json) for retained local exchanges, replies, handling outcomes and delivery status. Cover the period since the last heartbeat. Missing historical replies or delivery evidence are unknown. Retention and size limits apply; truncation and unavailable channels are explicit. Absence of a record never proves everything was handled.