{"slug": "how-to-split-work-between-an-llm-and-deterministic-code", "title": "How to split work between an LLM and deterministic code", "summary": "A developer outlined an approach for splitting extraction work between a language model and deterministic code when building an RDF graph of company acquisitions, having the model return only subject-predicate-object facts while code assigns stable entity IDs from document version and position. The writeup argues that fields the application already knows, such as document IDs, should be filled in by code rather than generated by the model to avoid errors, and recommends building labeled dev, validation and test sets to compare a full-graph prompt against a facts-only JSON prompt.", "body_md": "Suppose we’re building a graph database of company acquisitions. Each company is a node, and an arrow points from the buyer to the company it bought. We’re already asking a language model to read the announcements. Why not ask for the finished database record while we’re at it?\n\nTake the source document ID. If the model changes one digit, the right acquisition ends up attached to the wrong announcement. We already have the ID, so we can fill in that field ourselves. What else can we take out of the prompt?\n\nLet’s make up an announcement about two fictional companies.\n\nNorth Pier acquired Alder Labs.\n\nFinding “North Pier” and “Alder Labs” and marking both as companies is [named entity recognition](https://aclanthology.org/W03-0419/) (NER). Working out which company bought the other is *relation extraction*. That gives us the direction of the arrow.\n\nI made up the document number and the ID format for this example. The application already knows which document version and chunk it sends to the model.\n\nAn [RDF graph](https://www.w3.org/TR/rdf11-concepts/#section-rdf-graph) stores each fact as a triple with a subject, a predicate, and an object. We can ask the model for just the facts it finds in the sentence.\n\n```\n{\n  \"facts\": [\n    [\"North Pier\", \"type\", \"Company\"],\n    [\"Alder Labs\", \"type\", \"Company\"],\n    [\"North Pier\", \"acquired\", \"Alder Labs\"]\n  ]\n}\n```\n\nChange the sentence to “North Pier **was acquired by** Alder Labs.” The two company rows stay the same. The last row becomes `[\"Alder Labs\", \"acquired\", \"North Pier\"]`.\n\nCode assigns each mention an ID from the document version and its position within that document. On a retry, the same occurrence of “North Pier” gets the same ID.\n\nHere’s the resulting graph in TriG format. The last line links the extracted facts to their source chunk.\n\n```\n@prefix : <https://example.org/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix prov: <http://www.w3.org/ns/prov#> .\n\n:d17v1-c1-facts {\n    :d17v1-e0-10  a          :Company .\n    :d17v1-e0-10  rdfs:label \"North Pier\" .\n    :d17v1-e20-30 a          :Company .\n    :d17v1-e20-30 rdfs:label \"Alder Labs\" .\n\n    :d17v1-e0-10  :acquired  :d17v1-e20-30 .\n}\n\n:d17v1-c1-facts prov:wasDerivedFrom :d17v1-c1 .\n```\n\nThis works because each name occurs once in our chunk. If “North Pier” appears twice, we need more context to pick the right occurrence.\n\nBefore adding an acquisition, the code checks that both companies exist in the graph. That check passes whichever way the arrow points. To tell whether North Pier was the buyer, we have to compare the relationship with the original sentence.\n\nNorth Pier acquired the mill on lot 4.\n\nThe mill has no name here. Should it still get a node? For a directory of named facilities, we can skip it. For someone tracking which assets changed hands, the mill is the point. Our graph would need a `Facility` type for it.\n\nI’d write down what each user expects before changing the prompt.\n\n| What the text mentions | Named-facility directory | Asset register | \n|---|---|---|\n| West Mill, Pier Warehouse | Keep | Keep | \n| the mill on lot 4, the warehouse at berth 2 | Skip | Keep | \n| mills in general, warehouses in general | Skip | Skip | \n\nWhat about “North Pier met Alder Labs after it bought the mill”? Either firm could be the buyer. An `unresolved` list gives the model a way to flag “it bought the mill.” An empty list of acquisitions alone would look like a missed purchase.\n\nSuppose we change the prompt to keep unnamed facilities. It now picks up the mill on lot 4. Did it also start adding a facility every time someone writes “mills need water”?\n\nCollect several documents for each case and mark the expected answers. Keep revisions of one announcement in the same set. Edit the prompts on the development set, choose a version of each on the validation set, and save the test set for the final comparison.\n\nI’d compare two prompts. One asks for the full RDF graph. The other asks for the three-column JSON rows. Run each five times per test document with the same model, example sentences, and retry limit.\n\nCompare runs, too. A prompt can name the same wrong buyer five times.\n\nWe found the split by working backward from the result we wanted. If the application already has a value or can derive it by a rule, let code fill it in. Ask the LLM for the parts that require interpreting the input, then check its response in code. Test the rules directly. To see whether the model’s judgments match what users expect, try it on examples you kept out of prompt tuning.", "url": "https://wpnews.pro/news/how-to-split-work-between-an-llm-and-deterministic-code", "canonical_source": "https://dev.to/ksemianov/how-to-split-work-between-an-llm-and-deterministic-code-5cli", "published_at": "2026-11-28 00:03:26+00:00", "updated_at": "2026-11-28 00:14:11.530766+00:00", "lang": "en", "topics": ["large-language-models", "ai-agents", "structured-data", "natural-language-processing"], "entities": [], "also_reported_by": [], "takeaway": "Have the LLM return only extracted facts as subject-predicate-object triples and let deterministic code assign entity IDs from document version and position, so retries produce identical IDs and the model cannot corrupt known fields like document IDs.", "recommendation": {"advice": "Keep IDs and other known fields out of the prompt and fill them in code, then compare a full-graph prompt against a facts-only JSON prompt on labeled dev, validation and test sets.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/how-to-split-work-between-an-llm-and-deterministic-code", "markdown": "https://wpnews.pro/news/how-to-split-work-between-an-llm-and-deterministic-code.md", "text": "https://wpnews.pro/news/how-to-split-work-between-an-llm-and-deterministic-code.txt", "jsonld": "https://wpnews.pro/news/how-to-split-work-between-an-llm-and-deterministic-code.jsonld"}}