Dayward AIWeChat Mini Program
From Frontend Engineer to Agent Engineer in 30 Days
Week 2 · D13🔒 LockedAbout 6 hours

Cron Scheduling (Central Scheduler → Stream Delivery) + Cost Metering (Token → USD Ledger, Usage Report)

Implement a centralized cron scheduler that delivers scheduled tasks into the message bus, and build a cost-metering system that converts token usage into a USD ledger.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Implement a central scheduler that delivers tasks into the message bus on a cron schedule
  2. Implement a token-to-USD cost ledger, recording cost per call
  3. Produce a simple usage report that summarizes cost by user or by time

Yesterday fitted the agent with long-term memory and left a note in passing: every memory written calls an embedding, which is a new expense, and so far nobody is watching that bill. Today does both jobs — the agent goes from motionless until spoken to, to working on its own schedule, while every cent spent is recorded in a ledger as it goes.

Plain-Language Walkthrough

One person writes the duty roster; the staff do not each set an alarm

A company has three front-desk staff, and who starts when is written on the roster the office manager keeps. Change that to "each person sets their own phone alarm and opens up when it rings" and the result is not better punctuality but three people crowding the same door every morning, doing one job three times.

Since D10 you have had more than one worker: sharding and leases guarantee that one user's messages land on one worker, and the worker process itself is multi-replica, which D14 scales to 3. Write "run the daily digest task at 9am" into the worker's startup code and that line executes once per process — one task executed 3 times, users receiving 3 identical digests, and you paying three times.

Compute the money and it lands harder. Suppose 1,000 users in the support scenario have enabled a daily order summary, each running an agent at 9am: about 3,000 input tokens per request (system prompt plus tool definitions plus the day's order data) and about 500 output. At this course's standard prices, openai/gpt-4o-mini inputs at 0.15 dollars per million tokens and outputs at 0.60:

  • Input cost per call: 3000 / 1000000 * 0.15 = 0.00045 dollars
  • Output cost per call: 500 / 1000000 * 0.60 = 0.0003 dollars
  • Per call: 0.00075 dollars
  • Per month: 0.00075 * 1000 users * 30 days = 22.5 dollars

Twenty-two and a half dollars a month sounds harmless. But with three workers each running their own cron it becomes 67.5 — and the extra 45 dollars buys nothing but duplicate nuisance messages. Nor does it stop there: the day D14 moves replicas from 3 to 10, the bill and the nuisance both go up tenfold with no alert to anybody, because from each process's own point of view it merely executed dutifully on schedule.

So the correct shape gathers "who should be executed when" into one place, with workers responsible for working rather than for deciding when:

TextText
      +-------------+  cron matches  +-----------+  consumer group   +----------+
      |  scheduler  | -------------) | koda:runs | ----------------) | Worker 1 |
      | (single)    |  one XADD      |  the bus  | one message, one  +----------+
      +-------------+                +-----------+ consumer          | Worker 2 |
        does one thing:                                              +----------+
        decide what to send now                                      | Worker 3 |
                                                                     +----------+

The bus in that diagram is D9's: the scheduler is simply another producer for it, and the worker side does not change one line. A scheduled task is not a new kind of execution, it is a change of who presses the button — the user pressed it before and a clock presses it now. Grasp that and the amount of code today is startlingly small.

But handing the decision to one process immediately invites the question: what if the scheduler itself dies? If it restarts across the 9am minute, does it skip or duplicate? And if you run two scheduler instances for availability, are you not back to one task delivered twice?

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • When a service runs multiple replicas, why not let each replica start its own cron? What would you do instead?服务部署了多个实例,定时任务为什么不能让每个实例各自起一个 cron?你会怎么做?
    Common in ChinaCommon overseasBasic#scheduling#distributed-systems#cost

    🔒 Unlock this chapter to see the answer keys.

  • How would you design a token cost metering and ledger system from scratch?让你从零设计一套 token 成本计量和台账系统,你会怎么做?
    Common in ChinaCommon overseasIntermediate#cost#observability#data-modeling

    🔒 Unlock this chapter to see the answer keys.

  • Which dimensions should a usage report for an LLM product cover, and what decision does each one drive?一份 LLM 应用的 usage report 通常要覆盖哪些维度?这些维度分别用来做什么决策?
    Common in ChinaCommon overseasIntermediate#observability#cost#reporting

    🔒 Unlock this chapter to see the answer keys.

  • How do you keep a cron job from being published or executed twice, and how should the idempotency key be built?怎么保证一个 cron 任务不会被重复投递或重复执行?幂等键应该怎么构造?
    Common in ChinaCommon overseasDeep dive#idempotency#scheduling#distributed-systems

    🔒 Unlock this chapter to see the answer keys.

Comments