Gadi Cohen

How Granted's case-automation agent is built

Granted Health, 2025–now

I designed and lead the agent platform that resolves a patient's medical-billing case at Granted Health. A case can run for a long time. Documents arrive out of order, deadlines matter, and a wrong action costs the patient money, so most of the engineering went into the code around the model call.

IntakeDocumentsdenials, EOBs, itemized billsPatient messagesPayer responsesRead pipelineparses, classifies and extracts, with sourcesStateFact storeevent-sourced, one source of truth per caseContext engineplaybooks, gate state, scoped docsDecide and actPlannerbounded read-only loop, proposes one actionPreconditionschecked in codeHuman gateanything consequential waits for a personExecutoridempotent appeals, requests and messagesresults become eventsEvals and tracingboots this whole graph on seeded cases with side effects suppressed, and traces every decision
How a case moves through the system.

One case, end to end

Here's what happens to a single denial letter.

  1. 1

    A denial letter arrives

    The read pipeline parses it, classifies the denial, and extracts the facts that matter: codes, amounts, deadlines. Every extracted fact carries its source.

  2. 2

    Facts land in the store

    Each fact is stored as an event with what we learned, where it came from and how much to trust it, so we can replay the case's history from the log.

  3. 3

    The planner wakes up

    It reads the case through the context engine, which gives it the relevant playbook, the current gate state and the facts.

  4. 4

    It proposes exactly one action

    For example, it might propose requesting an itemized bill from the provider. The proposal is a typed object with the action, arguments, preconditions and an idempotency key.

  5. 5

    Code checks the preconditions

    Code checks things like whether the deadline is still open and whether we already asked. If a precondition fails, nothing happens.

  6. 6

    A person approves the consequential ones

    Sending an appeal or contacting a payer waits for human sign-off. The reviewer sees what the agent knew and why it proposed the action.

  7. 7

    The executor acts, once

    The idempotency key means a retry can't send two appeals. The executor appends the result to the fact store as a new event, and the loop continues.

  8. 8

    The whole run is traced and evaled

    The eval boots the same graph against seeded cases with side effects suppressed, and a regression shows up as a changed decision.

The contract between the model and the code

The model proposes actions, and code decides which ones can run.

// The planner can only return one of these.
// Code enforces everything else.
type ProposedAction = {
  kind: "request_itemized_bill" | "draft_appeal" | "message_patient" | /* … */;
  args: Record<string, unknown>;   // validated against the action's schema
  basis: FactRef[];                // which facts justified it, by event id
  preconditions: Check[];          // evaluated in code before execution
  approval: "auto" | "human";      // consequential ⇒ human
  idempotencyKey: string;          // a retry can never act twice
};
This is a simplified version. The production types have more fields.

Failures, and what catches them

We added each of these mechanisms after something broke.

Failure we hitWhat catches it now
Instruction X degrades instruction Y as the prompt growsContext registry: the agent reads what it needs per task; the prompt stays flat
The agent re-asks and contradicts itself across turnsEvent-sourced fact store: one source of truth, with provenance
You can't tell what the agent decided, let alone enforce itPlanner/executor split: every decision is a typed, logged proposal
A retry fires the same action twiceIdempotency keys at the executor boundary
An ambiguous document produces a confident wrong answerRetrieval answers carry scope; ambiguity escalates to a person
A prompt change breaks last month's cases and nobody noticesEvals boot the real graph on seeded cases and score decisions

Why it's shaped this way

The design came from a series of failures, in this order. The margin notes give the reason or the paper behind each change.

One prompt, one call

The simplest agent is one model call per turn: hand it the case, a set of tools, and a prompt telling it what to do. It holds up until you add the second capability, and the third. Each new rule expands the prompt, and at some point adding instruction X degrades instruction Y.“Never do X” rules measurably stop being followed as a conversation runs long (Constraint Decay, arXiv 2604.20911), while “always do X” rules hold near 100%. So we stopped putting everything in the prompt. The agent now reads context on demand from a registry that holds the relevant playbook and the current gate state.Close to what Agent-S (2503.15520) calls a global action repository, and to writing prompts as named, ordered routines (2501.11613). The prompt stays small and flat as the system grows.

Move the facts below the model

Now it reads context, but the facts of the case still live in the conversation, so the agent re-derives what it already knew every turn. It re-asks questions and contradicts itself. The fix was a canonical, event-sourced fact store where each fact records where it came from and how much to trust it.The pattern recent work calls event sourcing for autonomous agents (2602.23193), and the memory/compute split behind DeepSeek's Engram: the agent looks facts up instead of recomputing them. The agent reads from one source of truth instead of reconstructing it each time.

Separate deciding from doing

At this point the agent still decides and acts in one step, and the action is whichever tool it happened to call. You can't enforce a precondition in a prompt or measure what it decided. So we split proposing from doing. A bounded planner proposes one action in a short, read-only loop, and a single executor carries it out. Code checks the preconditions, and anything consequential waits for a person.Rebuilding the agent as a deterministic state machine looks more reliable, but it moves the non-determinism into the resolver and the queue. We kept the agent and enforced the guarantee at the source of truth.

Make failure visible

We also needed a way to catch this breaking. Tool-match and similarity scores miss failures like asking to escalate twice or inventing a fact. So the eval boots the real execution graph against seeded data with side effects suppressed, and it scores the decision the agent made.The simulacrum idea behind τ-bench (2406.12045); on methodology, Anthropic's Demystifying Evals (pass^k over whole trajectories).

Built on the Vercel AI SDK and Claude, with Langfuse for tracing, Vellum for retrieval, an event-sourced fact store on Gel, and durable execution on Temporal.