cleandataengineering.aiDrug-Safety Agent on Databricks, drawn simply
You are here: End to end

Drug-Safety Agent on Databricks · End to end

Follow one question end to end

A question takes one of four paths, and the first thing that happens is a set of plain rules, not a model. Only a real investigation reaches the loop. Everything that must be refused or handed to a person is decided before any model is paid for.

Goes inA sentence from a scientist, plus who they are
What happensThe router picks one of four paths. Only the investigate path calls a model, then checks run on its draft
Comes outA reply: a refusal, a hand-off, a direct answer, or a checked draft
Every box opens its page. The queue and the hand-off are on the desk.

Why the router goes first. The most harmful mistake this agent could make is a draft that reads like a finding. Putting the refusals in front of the model means no clever wording can talk the agent into one. It also saves money: a plain count never needs a model.

One front door. Every request enters through a single function, handle, so no path can skip the rules. Read the router next, or jump to all steps in one table or one real run.

The real code

Cut from the repository each time this site is built, so it always matches what runs. Each header names the file and the line it was cut from.

src/drugsafety/entrypoint.py:85handle(): the whole agent as a caller sees it
async def handle(
    text: str, *, reviewer_id: str, llm, tools=None, store: AssessmentStore | None = None, source: str = "desk"
) -> Reply:
    """`store` is the desk: with it, a checked draft is queued for a reviewer and a hand-off is recorded for a person."""
    who = identity_for(reviewer_id)
    if tools is None and store is not None:
        tools = LocalWorld(store)
    r = route(text)
    if r.answered_by == "refusal":
        return Reply(REFUSALS[r.refusal or ""], r)
    if r.answered_by == "handoff":
        eid, new = _escalate(store, "asked-for-human", text, reviewer_id)
        if eid is None:
            return Reply(HANDOFF, r)
        return Reply(HANDOFF + (f" Reference {eid}." if new else f" It is already with a person: {eid}."), r, escalation_id=eid)
    if r.answered_by == "direct":
        return Reply(await _direct(r, tools, who), r)
    if r.intent == "investigate":
        return await _investigate(r, text, reviewer_id, who, llm, tools, store, source)
    return Reply(
        "I investigate whether a drug and an adverse event form a signal in public reports. Name a drug and an event, "
        "or ask for the report counts or whether the label mentions it.",
        r,
    )

Look inside