Skip to main content
Glama
LLM Liquid Handler

LLM Liquid Handler

README.md
<p align="center"><img src="docs/banner.png" alt="LLM Liquid Handler" width="100%"></p>

<p align="center">
  <a href="https://github.com/kawtar-bellamine/llm-liquid-handler/actions/workflows/tests.yml"><img src="https://github.com/kawtar-bellamine/llm-liquid-handler/actions/workflows/tests.yml/badge.svg" alt="tests"></a>
  <img src="https://img.shields.io/badge/python-3.11%2B-blue" alt="python">
  <img src="https://img.shields.io/badge/MCP-server-0f8b8d" alt="MCP">
  <img src="https://img.shields.io/badge/Opentrons-Flex%20API%202.20-14213d" alt="Opentrons">
  <img src="https://img.shields.io/badge/licence-MIT-lightgrey" alt="MIT">
</p>

**A scientist types a request in plain English; a language model turns it into a liquid-handling protocol. This
project is a proof of concept of how to let that happen safely.** The model never writes robot code: it writes a
small JSON plan through **MCP tools**. Code checks the plan against the deck in five layers, does the arithmetic
(dilution series, normalisation), compiles it into an **Opentrons Flex** protocol, runs the **official Opentrons
simulator**, reads the plan back in plain words with plate maps, and produces a protocol only after a **scientist
approves** it, with an audit record. A study of 640 injected model errors measures what each guardrail catches and
what only a human can.

<p align="center"><img src="docs/tour.gif" alt="Dashboard tour" width="100%"></p>

## Results at a glance

| Question | Answer |
|---|---|
| Is the Opentrons simulator enough as a safety net? | **No.** Generating the code and running the simulator rejects **38 %** of 640 faulty plans. It catches invented labware, wells and slots, but **none of the 192 volume errors**: 400 µL into a 360 µL well, 100 mL instead of 100 µL, 1 µL below the pipette minimum all simulate without error. |
| What do checks against the deck add? | The five layers together reject **75 %**: every structural, identifier, tip-handling and unit (mL read as µL) error, and **92 %** of volume errors. A full check takes **under 1 ms**, against 0.1 s for a simulation, so the model can iterate. |
| What can no automatic check catch? | **162 faulty plans (25 %) pass every layer**: the wrong reagent, one column off, 1:2 instead of 1:3, a step left out, a volume divided by 10. They are physically valid and only differ from the *intent*. That is why the plan is read back to the scientist, in words written by code, before approval. |
| Should the model do the arithmetic? | No. When the model computes the normalisation volumes itself with one wrong assumption, **12 of 12** faulty plans pass every check. In this design the model states the target (1 mM in 150 µL) and code computes 12.1 to 45.5 µL of stock from the concentrations on record. |
| How far do fixed rules go for the language? | A deterministic grammar gets **60 of 60** requests right in the phrasing it was written for, but only **5 of 60** paraphrases; it asks back on 52 and produces **3 wrong but valid plans**. Of 40 requests that must not run as written, it asks back on 23 and the guardrails reject 17. The same harness scores a language model with one command. |

## 1. Architecture

![Architecture](results/figures/fig1_architecture.png)

* `labagent/server.py`: the **MCP server**, the only door to the robot. Tools: `get_deck`, `get_plan_schema`,
  `compute_normalization`, `check_plan`, `simulate_plan`, `request_run`. Any MCP client can use it (Claude Desktop,
  an IDE agent, the agent loop in `labagent/agent.py`).
* `labagent/plan.py`: the **plan schema** (transfer, serial dilution, normalisation, mix, delay) and the well
  selectors (`plate:column 3`, `plate:A1..A6`). Labware geometry comes from the **official Opentrons labware
  definitions**, so a well or a capacity is never invented.
* `labagent/engine.py`: plan to atomic operations (pick-up, aspirate, dispense, mix, drop) and a **liquid-state
  model** that replays them: volume and composition of every well, dead volumes, tips, contamination.
* `labagent/compile.py`: operations to an Opentrons Flex protocol (API 2.20), and the simulator run. The robot
  executes exactly the operations that were checked.
* `request_run` asks the scientist through **MCP elicitation**; nothing is written without a yes, and every approved
  run leaves the protocol and a JSON audit record (plan, read-back, who, when).

## 2. The read-back

![Showcase](results/figures/fig2_showcase.png)

Three 11-point dilution series, six stocks normalised to 1 mM and a metabolic-stability set-up: 391 pipetting
operations, 48 tips, 679 robot commands in the simulator. The concentrations come from the liquid-state model,
not from the language model.

## 3. Guardrail study

![Guardrails](results/figures/fig3_guardrails.png)

`bench/mutations.py` injects 15 kinds of plausible model errors into 60 correct plans (five workflows of a
drug-metabolism lab: reagent addition, serial dilution, stock normalisation, stability set-up, plate copy). Each
layer is run on its own on every faulty plan. 14 mutants that change nothing (a tip reused on the same liquid, a
delay removed) are left out.

![Defence in depth](results/figures/fig4_defence.png)

## 4. Natural language

![Language](results/figures/fig5_language.png)

160 requests (`bench/requests.jsonl`): 60 canonical, the same 60 paraphrased, 40 that must not run as written
(missing volume, unknown liquid, wells that do not exist, overfill, infeasible normalisation, ambiguous labware).
Plans are scored **by outcome, not by text**: both plans are replayed and every well must end with the same liquids
to 0.1 µL. The baseline is a deterministic grammar (`labagent/nl.py`), not a language model: no language-model score
is reported here because none was run. To score one:

```bash
export ANTHROPIC_API_KEY=...  ANTHROPIC_MODEL=<model id>
python -m bench.evaluate --backend anthropic
```

## Run it

```bash
pip install -r requirements.txt
pip install --no-deps -r requirements-opentrons.txt   # Opentrons pins an old jsonschema that MCP cannot use
python experiments/run_all.py                         # full study, about 2 minutes
streamlit run app/dashboard.py                        # bench console
pytest -q                                             # 42 tests
```

Connect the server to Claude Desktop (or any MCP client): see [`docs/mcp_setup.md`](docs/mcp_setup.md).

<table>
<tr><td width="50%"><img src="docs/screenshots/console.png" alt="Bench console"></td>
<td width="50%"><img src="docs/screenshots/blocked.png" alt="Blocked request"></td></tr>
</table>

## Repository map

```
labagent/plan.py       plan schema and well selectors
labagent/hardware.py   deck, pipettes, labware from the official Opentrons definitions
labagent/engine.py     atomic operations and liquid-state model
labagent/validate.py   the five guardrail layers
labagent/compile.py    Opentrons Flex protocol and simulator
labagent/summary.py    read-back and plate maps
labagent/server.py     MCP server
labagent/agent.py      LLM agent loop over the MCP tools
labagent/nl.py         deterministic baseline grammar
bench/                 requests, error injection, scoring, evaluation CLI
experiments/           full study and figures
app/dashboard.py       Streamlit bench console
examples/              showcase plan and its generated protocol
```

## Limits and next steps

* Tested in simulation only. The next step is a real liquid handler: upload the approved protocol to an Opentrons
  robot through its HTTP API, or write an adapter for another vendor's software, and measure on the instrument.
* The liquid-state model assumes perfect pipetting and a fixed dead volume per labware type (an editable assumption
  in `labagent/hardware.py`); it does not model viscosity, evaporation or carry-over amounts.
* The benchmark requests and the error types were written for this study. They show where each guardrail works;
  they do not measure how often a given language model makes each error. `bench/evaluate.py` measures that.
* Single-channel pipettes only; no modules (heater-shaker, thermocycler, magnetic block) yet.

## Licence

MIT. Opentrons labware definitions and simulator: `opentrons` and `opentrons-shared-data` packages (Apache 2.0).