cleandataengineering.aiDrug-Safety Agent on Databricks, drawn simply
You are here: Core loop

Drug-Safety Agent on Databricks · Core loop

The loop: gather facts, then draft

The loop is the heart of the agent. On each trip it asks the model one question. If the model wants a fact, the loop runs the tool and goes round again. If the model answers in words, the loop ends and the checks begin. The loop itself is not written here: it comes from the shared library, and this agent supplies the tools, the standing instruction and the limits.

Goes inA drug, an event, and the standing instruction
What happensUp to 6 trips of: ask the model; if it asks for a tool, run it and add the result
Comes outA JSON draft, or a clear reason the loop stopped
The ring repeats once per tool call. A typical investigation: two tool calls, then the draft.

The model only ever asks. It never runs a tool itself. It writes "please call signal_stats for lisinopril and angioedema", and the loop decides whether that is allowed, runs it, and hands back the result. That one fact is what makes every check possible.

Why a loop at all. For a fixed question like this, a plain script could call all three tools in order. We use a loop so the same agent can handle variations (an unusual pair, a tool error, a request that needs one more look) without a new script for each. The cost of that flexibility is why we add limits and checks.

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/hagent.py:87investigate(): one turn through the shared loop, then parse, repair once, return a Draft
async def investigate(
    llm, drug: str, event: str, *, budgets: Budgets = BUDGETS, tools=None, identity: Identity = IDENTITY, source: str = "agent"
) -> Draft:
    """`tools` is a tool client (an AgentTwin projection, or the Unity Catalog functions); None reads the local files."""
    seen: list[dict] = []
    facade = PairTools(tools, seen, source=source) if tools is not None else None
    local = None if facade else local_tools(seen)
    goal = f"Investigate: drug={drug!r}, event={event!r}."
    steps, spend = 0, 0.0

    async def turn(prompt: str, history: tuple[Message, ...] = ()):
        nonlocal steps, spend
        ended, trace = await loop.run(
            prompt,
            identity=identity,
            llm=llm,
            tools=facade,
            system_prompt=SYSTEM,
            budgets=budgets,
            local_tools=local,
            history=history,
        )
        steps, spend = steps + trace.steps, spend + trace.spend_usd
        return ended

    def done(status: str, body: dict | None, reason: str = "", ended: object = None) -> Draft:
        return Draft(
            status, body, reason, trace=seen, termination=str(getattr(ended, "termination", "")), steps=steps, spend_usd=spend
        )

    result = await turn(goal)
    if not isinstance(result, Completed):
        return done("needs_human", None, f"the loop ended without an answer: {_why(result)}", result)
    body, problems = _parse(result.reply)

Look inside