The Ops Community ⚙️

Chen Yifeng
Chen Yifeng

Posted on

Separating Deterministic Calendar Math from LLM Generation: Architecture Notes from Building a BaZi Engine

When teams wire an LLM into a domain that depends on exact astronomical or calendar rules, the first prototype almost always fails in the same way: the model sounds fluent while silently inventing the underlying math.

We ran into this immediately while building a Chinese metaphysics and classical calendar platform. Ask a frontier LLM directly to compute a BaZi (Four Pillars of Destiny) chart from a Gregorian birth date, time, and city, and it will often get the Year Pillar right, miss the Month Pillar around a solar-term cutoff, hallucinate the Day Pillar completely, and ignore local longitude correction for the Hour Pillar.

The fix is not a longer system prompt. It is a hard architectural boundary between deterministic calculation and language generation.

Why LLMs Fail at Stem-and-Branch Calendars

A Four Pillars chart is a coupled system of four sexagenary pairs (Heavenly Stem + Earthly Branch), each governed by a strict boundary rule:

  1. Year and Month Pillars follow solar terms, not lunar months. The year changes at Lichun (315° solar ecliptic longitude), a single UTC instant worldwide—not at Lunar New Year. Near early February, a naive lookup table or LLM training prior frequently assigns the wrong year branch.
  2. The Hour Pillar depends on True Solar Time. Standard civil timezones can diverge from local solar noon by 30 minutes to over an hour depending on longitude, equation of time, and historical daylight saving windows (such as 1986–1991 in China or 1948–1951 in Japan). Near an odd-hour boundary (11:00, 13:00, 23:00), civil time shifts the Hour Pillar entirely.
  3. Derived structures cascade from the base pillars. Ten Gods, hidden stems, ten-year luck cycles (Dayun), and annual interactions all depend on exact upstream stem-and-branch indices. One off-by-one error in the day stem invalidates every downstream interpretation.

The Two-Layer Pipeline: Deterministic Core + Constrained Context

Instead of asking a model to "calculate and interpret," we split the workload into two isolated layers:

Layer 1: Client-Side Deterministic Engine

All ephemeris math, solar-term boundary comparisons, longitude-based true solar time conversion, and pillar derivation execute deterministically in the browser. Running this layer locally has two operational advantages:

  • Zero-hallucination invariants: The exact UTC timestamp of each solar term and the pre-/post-correction local times are verifiable outputs, never sampled tokens.
  • Privacy by architecture: Users who only want the calculated chart do not have to send birth timestamps or locations to a backend database. You can see this deterministic front end in action on Taoly's browser-based BaZi calculator.

Layer 2: Structured JSON Grounding for Optional LLM Readings

When a user opts into an AI interpretation, the LLM never receives just a raw birthday string. It receives the serialized, pre-validated chart object—explicit stems, branches, hidden stems, Ten Gods, luck-cycle start age, and relevant classical source citations—along with a strict instruction not to recompute any calendar field.

Even simple guardrails pay off quickly:

  • Schema-locked inputs: Pass canonical stem/branch tokens and pre-computed relationships so the model only synthesizes explanations.
  • Boundary metadata as first-class fields: Include explicit flags when a birth falls near a solar-term transition (for instance, the two-day gap in early 2027 between Lichun on February 4 at 01:46:18 UTC and Lunar New Year on February 6, which we document in our 2027 Chinese Zodiac & Tai Sui reference).
  • Regression test suites on edge dates: Maintain unit tests around midnight Zishi rollovers, leap months, DST ranges, and western-longitude cities where solar terms cross into the previous local calendar day.

Operational Takeaway

In production AI systems, reliability rarely comes from pushing an LLM to act like a calculator. Pushing every deterministic rule—timezones, astronomical cutoffs, state transitions—into testable code and limiting the model to grounded synthesis makes failures reproducible, keeps latency predictable, and stops subtle math bugs from hiding behind confident prose.

Top comments (0)