用 Agent SDK 搭建常驻 AI Agent 监控 Hacker News
Key Highlights
OpenRouter published a practical tutorial showing how to build an "always-on" AI agent with TypeScript and the @openrouter/agent 0.11.0 SDK. The canonical scenario: wake on a schedule (default every 15 minutes), check user-selected Hacker News topics, write a digest of worth-reading stories, remember seen items across restarts, and cap each run's cost at five cents via maxCost(0.05).
What Happened
The tutorial defines three tools, each described with a Zod input schema: check_hacker_news (calls the HN search API for unseen stories), write_digest (appends relevant stories to digest.md), and post_to_team_channel (sends to a team channel but flagged requireApproval: true for human approval). The agent is built around four essentials: a scheduling loop, tools for perception and action, memory that survives crashes (disk files), and limits (step and cost caps plus approval for externally consequential actions).
Technical Details
Persistence uses seen.json (the set of processed story IDs) and state.json (conversation state saved via StateAccessor, letting a paused run resume in another process); newly fetched IDs go to fetched.json first to survive a mid-run crash, merging into seen.json only on success. Limits use stopWhen: [stepCountIs(6), maxCost(0.05)] to terminate over-budget runs between tool turns; external-action tools use requireApproval, pausing the run and writing the pending call to pending.log, which a user resolves via decisions.json before the next run resumes.
Versus Competitors
Against running agents inside a single conversation, this tutorial emphasizes an engineering pattern of "always-on plus persistent memory plus cost guardrails," closer to treating the agent as a continuously running microservice. It is model-agnostic, recommending that you start with a small tool-calling model (the tutorial uses anthropic/claude-haiku-5.5) rather than a heavyweight flagship.
Industry Impact and Use Cases
For developers who want "background watchdog" agents — monitoring competitor moves, generating periodic briefs, or running automated inspections — this tutorial is a ready-to-use scaffold. Measured cost is two to three model calls per check, spending $0.0003 to $0.0012, which works out to roughly $0.86 to $3.46 per month — proof that an always-on agent can be both cheap and controllable.
Practical Notes
The design choices here are what make it production-safe rather than demo-cute. The separation of fetched.json (tentative) from seen.json (committed) means a crash mid-loop loses at most one batch, not the whole memory. StateAccessor is the linchpin: it is what lets you kill the process, reboot the machine, and have the agent pick up exactly where it left off. If you adapt this pattern, the single most important thing to copy is the crash-then-resume discipline, because a watchdog that forgets its state after a reboot is just a cron job that occasionally hallucinates.
What to Watch
Two caveats. First, the five-cent cap is a hard stop, but it is enforced only between tool turns; a single runaway tool call could still overshoot before the limiter fires, so set your model's own per-request caps as a backstop. Second, requireApproval is only as good as the human watching the pending.log — if nobody reviews it, the "approval gate" becomes a silent stall. Treat the log as an inbox that must be drained, not a formality.
Closing
This tutorial is a small but well-formed template for the boring, unsexy part of agent engineering that actually determines whether something survives contact with production: memory that survives crashes, costs that cannot spiral, and actions with real-world consequences that wait for a human. Most "agent" hype is about capability; this is about reliability, and reliability is what separates a toy from a tool.
The Bigger Picture
The always-on agent pattern is quietly becoming the default shape of useful automation. Most "AI agent" demos are single-shot; the ones that earn a place in a production stack are the boring ones that wake up, do a small job, record what they did, and go back to sleep without human babysitting. OpenRouter's tutorial is valuable precisely because it shows the seams — the memory file, the cost limiter, the approval gate — that separate a demo from a daemon. If you build one, design for the unglamorous ninety-nine percent of runtime when nothing interesting happens, because that is exactly where reliability is won or lost, and where most agent projects quietly die.