Legal Prompt Engineering for PPMO Nepal Compliance
Agentic Prompt Engineering for Legal Precision: Architecting Deterministic JSON Outputs for Public Procurement Compliance
The deployment of large language models (LLMs) within legal technology and public administration fundamentally alters the landscape of document automation, contract drafting, and compliance monitoring. However, the transition from experimental conversational interfaces to enterprise-grade, agentic systems requires a paradigm shift. In domains such as public procurement, where a misplaced comma or an omitted statutory reference can invalidate a multi-million-dollar contract or expose a public entity to severe financial liability, the stochastic nature of autoregressive text generation is a critical vulnerability.
LLM outputs in software engineering and legal drafting rarely exist in isolation; they must seamlessly integrate into toolchains, APIs, and data pipelines that impose strict structural contracts. A semantically correct legal clause that violates the expected data format is, from the consuming system’s perspective, indistinguishable from a wrong answer. To achieve operational viability, artificial intelligence systems must be strictly constrained. They must operate within a deterministic runtime harness that forces the model to produce strictly mapped, machine-readable data—typically JavaScript Object Notation (JSON)—rather than conversational text.
This comprehensive analysis explores the intersection of agentic prompt engineering, constrained decoding, and legal precision. By examining the specific regulatory framework of the Government of Nepal’s Public Procurement Monitoring Office (PPMO), the analysis demonstrates how rigid structural constraints (JSON schemas), few-shot prompting, and programmatic validation loops can eradicate hallucinations and ensure that every generated clause aligns immutably with statutory requirements.
The Illusion of Determinism in Probabilistic Models
An LLM is fundamentally a probabilistic engine designed to calculate the mathematical likelihood of the next token in a sequence. Because the foundational training corpora for these models contain vast amounts of conversational text and internet tutorials, the models possess a strong inherent bias toward verbosity. For instance, when instructed to output a JSON object, the model frequently precedes the data with conversational filler, such as introductory markdown or conversational explanations, simply because that pattern was mathematically dominant in its training data.
In an enterprise software environment, even a one percent failure rate in structural fidelity is catastrophic. A stray comma, an unclosed bracket, or a hallucinated key can crash parsers, cascade errors through automated data pipelines, and corrupt downstream legal databases. Furthermore, developers frequently observe that outputs vary for the same inputs under settings expected to be deterministic. Empirical testing of API-based LLMs configured with a temperature parameter of zero reveals accuracy and output variations of up to fifteen percent across multiple runs. This instability stems from opaque backend mechanisms in hosted environments, such as input buffer packing across multiple jobs and floating-point non-determinism in parallelized hardware architectures. The evaluation of model determinism relies on metrics such as the Total Agreement Rate across runs (TARr@N), which consistently shows that no LLM delivers perfectly identical outputs over time regardless of the task.
Therefore, achieving legal precision requires acknowledging that an LLM agent is not merely an isolated neural network; it is a software system embedded within a stateful environment. The behavior of the agent is dictated by the runtime harness that mediates observation, tool use, action execution, and trajectory control. To force deterministic behavior in a “vibe-coded” agentic environment—where the developer relies on the model’s semantic intuition to solve complex tasks—architects must shift their focus from the model’s parameters to the interaction interface. By utilizing techniques such as constrained decoding and programmatic validation, the underlying volatility of the language model is neutralized, allowing its powerful reasoning capabilities to be safely harnessed for legal drafting.
Architecting Structured Outputs and Constrained Decoding
To eliminate the probabilistic variance of text generation, modern agentic systems employ structured outputs, also known as constrained decoding. This approach fundamentally alters the generation process at the inference level, removing the model’s ability to deviate from a predefined programmatic blueprint. The progression of output control has evolved significantly, moving from fragile prompt engineering to rigid mathematical token masking.
The Evolution of Output Control: From Prompting to Native JSON
Initially, developers relied heavily on prompt engineering, writing exhaustive system instructions explicitly demanding valid JSON, forbidding markdown, and specifying null values for missing fields. This approach often failed because it relied on the model’s semantic understanding of the instructions rather than enforcing actual computational boundaries. The introduction of native JSON Mode by major model providers mitigated basic syntax errors by forcing the model to balance curly braces and brackets, but it did not guarantee adherence to a specific schema or prevent the hallucination of unauthorized data fields.
The current industry standard is true Structured Output, which relies on JSON Schema validation and function calling. When a strict schema is passed into the API call, the runtime environment generates a token mask. The engine calculates the legal tokens for every single step of the generation; if the schema dictates that the output must begin with an object, the engine mathematically reduces the probability of all tokens other than the opening curly brace to zero. This schema-first approach delivers highly reliable type-safety, programmatically detectable refusals, and simpler prompting, as strongly worded formatting instructions are no longer necessary. The output conforms strictly to defined object structures, nested hierarchies, and property constraints.

Grammar-Based Decoding and Finite-State Machines
For open-source models and self-hosted infrastructure, grammar-based decoding frameworks provide unparalleled, deterministic control. Libraries such as Outlines convert schemas into context-free grammars (CFGs) or Extended Backus-Naur Form (EBNF) notation. These grammars are compiled into finite-state machines that guide token selection dynamically during generation.
This technique prunes invalid paths during beam search and utilizes finite automata for valid token selection, ensuring that any token leading to an invalid sequence is completely disallowed from the model’s vocabulary at that specific step. The implementation of grammar compilation offers profound architectural advantages for legal technology:
- Token Masking: Prevents generation of schema-violating characters (e.g., trailing commas, unclosed strings). Eliminates syntax-based parsing errors and the need for regular expression cleanup scripts.
- Zero Inference Overhead: Finite-state machines are pre-compiled before the first token is generated. Adds virtually no computational cost during the inference phase, ensuring low latency.
- Path Pruning: Simplifies the model’s decision-making process by removing invalid generation branches. Can increase generation speed by up to five times compared to standard autoregressive generation.
- Recursive Schemas: Supports deeply nested data structures essential for complex legal contract representation. Achieves 98 percent schema adherence, vastly outperforming post-generation validation pipelines.
By utilizing JSON Schemas or EBNF grammars, the LLM is transformed from a stochastic text generator into a deterministic compiler of structured legal data.
Data Modeling with Pydantic and Instructor
In Python-based agentic workflows, JSON schemas are rarely written by hand, as manual creation introduces human error. Instead, they are generated dynamically using data validation libraries like Pydantic. Pydantic allows architects to define the expected output as strongly typed data classes, translating complex object-oriented requirements into the strict JSON schemas required by the LLM APIs.
Libraries such as Instructor act as lightweight wrappers around LLM clients, injecting these Pydantic models into the API request as function-calling schemas. Pydantic models enable the definition of clear field names, comprehensive descriptions (which serve directly as prompt instructions to the model), type constraints, and complex nested structures. Most critically, Pydantic facilitates automatic retry loops. If the LLM generates an output that violates a business logic validator, the library catches the validation error, packages the error context, and re-prompts the model. This allows the LLM to observe its specific mistake and correct the output without crashing the end-user application.
The Role of Few-Shot Prompting in Structural Mimicry
While constrained decoding enforces syntax and schema rigidity, the semantic quality, legal phrasing, and stylistic tone of the generated text rely heavily on advanced prompt engineering, specifically few-shot prompting. Describing the desired tone and structure of a complex legal clause in abstract prose is highly inefficient and prone to misinterpretation.
Showing the model direct examples of the desired input-output pattern is universally recognized as the most effective method for aligning AI behavior with strict institutional expectations.
Token Efficiency and Reduced Hallucination
Few-shot prompting significantly minimizes context window usage compared to zero-shot verbose instructions. Research indicates that providing three well-crafted few-shot examples (consuming approximately 150 tokens) drastically outperforms a 600-token verbose instruction block, saving substantial input costs while delivering superior output quality. Without concise examples, developers are forced to write lengthy prose instructions that consume more tokens, dilute the primary directive, and introduce semantic ambiguity.
In the context of legal clause generation, few-shot examples anchor the model’s behavior. If the agent is tasked with drafting a custom variation clause for a construction contract, providing two examples of fully compliant JSON payloads teaches the model implicit patterns regarding formatting, the use of passive legal terminology, and the appropriate level of contractual detail. This structural mimicry ensures that the generated text seamlessly matches the surrounding boilerplate language of the document.
Chain-of-Thought Extraction for Legal Reasoning
Combining structured outputs with few-shot prompting and Chain-of-Thought (CoT) reasoning yields optimal results for complex legal analysis. By instructing the model to break down problems step-by-step and “think aloud” before arriving at a final answer, accuracy on complex reasoning tasks is substantially improved.
In a structured JSON schema, this is implemented programmatically by requiring a reasoning or chain-of-thought string field to precede the final output fields. Because LLMs process information autoregressively, they can only “see” the tokens they have already generated. If an LLM is forced to output a definitive boolean flag indicating whether a contract is compliant as the very first token, it must make a single-leap assumption. Conversely, if the schema forces the model to first generate a text block explaining its legal analysis, the tokens generated in that reasoning field serve as extended context for the subsequent decision, preventing premature conclusions and significantly reducing hallucinations.
Architecting PPMO-Compliant Legal Contracts
To understand the practical application of agentic prompt engineering, it is necessary to examine a highly regulated domain: the public procurement framework of Nepal. The Government of Nepal’s Public Procurement Monitoring Office (PPMO) enforces strict guidelines, rooted in the Public Procurement Act 2063 (PPA) and Public Procurement Regulations 2064 (PPR), to ensure transparency, competition, and accountability in the expenditure of public funds.
The Standard Bidding Document (SBD) Architecture
The PPMO mandates the use of Standard Bidding Documents (SBDs) for the procurement of goods, works, and consulting services. The structural integrity of these documents is paramount, and any unauthorized deviation renders a procurement process void. According to PPMO guidelines, the SBD is divided into immutable and mutable sections.
The Instructions to Bidders (ITB) and the General Conditions of Contract (GCC) contain standard provisions designed to remain entirely unchanged. Public Entities are strictly forbidden from modifying the text of the ITB or GCC under any circumstances. Conversely, project-specific information, modifications, and supplements to the standard conditions must be explicitly confined to the Bid Data Sheet (BDS) and the Special Conditions of Contract (SCC).
This rigid legal architecture maps perfectly to object-oriented programming and JSON schema design. An AI agent tasked with drafting a bidding document must never be permitted to generate the entire text from scratch. Instead, the agent’s schema must treat the GCC and ITB as frozen baseline data, while restricting generative output strictly to the key-value pairs required for the BDS and SCC. By defining a Pydantic model that only accepts fields relevant to the SCC—such as the employer’s name, the intended completion date, and insurance deductibles—the runtime harness physically prevents the LLM from hallucinating unauthorized changes to the General Conditions of Contract. This guarantees that the generated document remains legally sound and tightly aligned with PPMO standards, ready for seamless upload into the national e-GP (electronic Government Procurement) portal, Bolpatra.
Case Study 1: Deterministic Drafting of Liquidated Damages (Rule 121)
Under the Nepalese public procurement regime, Liquidated Damages are strictly regulated to penalize contractor delays without serving as unconscionable penalties. Rule 121 of the PPR dictates that if a contractor fails to complete works within the specified time, they must pay the Public Entity liquidated damages at a strict rate of 0.05 percent of the contract price per day of delay. Crucially, the total accumulated liquidated damages are legally capped and cannot exceed 10 percent of the total contract price.
An unstructured, conversational LLM tasked with drafting a penalty clause might easily hallucinate standard commercial rates common in its training data, such as a one percent weekly penalty, or it might omit the ten percent statutory cap entirely. Such hallucinations would expose the document to immediate legal challenge during arbitration at the Nepal Council of Arbitration (NEPCA). To ensure deterministic compliance, the agentic prompt engineering utilizes a strict JSON schema combined with logic validation.
By utilizing the const keyword in the JSON schema, the LLM is mathematically forced to output exactly the statutory values. The generative capacity of the model is restricted solely to a custom text field, where it drafts the contextual language required for the specific project, knowing that the numerical boundaries are guaranteed by the schema itself.

Case Study 2: Variation Orders and the Mahakali Precedent
Variations in public contracts—involving changes to the scope, quantity, or design of the original works—are highly sensitive areas prone to corruption, budget overruns, and prolonged disputes. Section 54 of the PPA strictly tiers the authority to approve variations based on cost percentages relative to the initial contract price.
| Variation Amount | Eligible Approving Authority | Legal Requirement |
|---|---|---|
| Below 5% | Gazetted 2nd Class Officer (or equivalent in-charge) | Must not be lower in rank than the official who approved the initial cost estimate. |
| Below 10% | Gazetted 1st Class Officer (or equivalent in-charge) | Requires documented justification of necessity. |
| 15% to 25% | Secretary of Ministry or Departmental Head | Requires rigorous technical review and recommendation. |
| Above 25% | Council of Ministers (Cabinet) | Mandates recommendation provided by a specialized group of experts. |
This hierarchy is not merely procedural; it is absolute. In the landmark case Mahakali Irrigation Project vs. Swachchhanda Nirman Sewa Pvt. Ltd. (NKP 2066, Decision No. 8156), the Supreme Court of Nepal established a rigid precedent regarding constructive variations. In this case, the contractor performed extra work beyond the Bill of Quantities based on oral instructions from site engineers. The Supreme Court denied payment, ruling that variations lacking a formal, written order from the exact eligible authority mandated by Section 54 cannot create a financial liability for the state. The court prioritized strict procedural legality over physical performance, effectively eliminating “No Oral Modification” loopholes.
When an AI agent evaluates contractor claims or drafts method statements involving variations, it must be programmatically constrained against approving unwritten directives based on equitable principles. The prompt engineering must embed a “Reservation of Rights” protocol directly into the code. Using a Pydantic model, developers can write a @model_validator that intercepts the LLM’s output. If the LLM attempts to validate an unwritten variation because “the work was physically completed,” the validator will intercept the output, throw a ValueError, and force the LLM to retry the generation with the legal context of the Mahakali precedent appended to the error message. This ensures the agent perfectly mirrors the Supreme Court’s jurisprudence.
Case Study 3: E-GP Integration and Performance Security Formulas
To mitigate the risk of contractors submitting abnormally low bids (front-loading) to win a tender and subsequently abandoning the project, the PPMO enforces dynamic performance security calculations.
If the bid price of the selected bidder is up to fifteen percent less than the approved cost estimate, the standard performance security is simply five percent of the bid price. However, if the bid is more than fifteen percent below the cost estimate, the performance security must be calculated using a specific statutory formula to cover the state’s elevated execution risk. The formula requires adding five percent of the bid price to half of the difference between eighty-five percent of the cost estimate and the bid price.
An agent generating a Letter of Acceptance or formulating the SCC must output the correct financial requirements for the winning bidder. Rather than prompting the LLM to perform complex floating-point arithmetic—a task at which language models inherently struggle and frequently hallucinate—the agentic architecture utilizes Tool Calling. The LLM is instructed to extract the exact bid price and cost estimate into a structured JSON payload.
A deterministic Python function within the runtime harness executes the statutory formula, and the exact, mathematically flawless result is passed back to the LLM to populate the final legal document. This separation of concerns—using the LLM for semantic extraction and deterministic code for mathematical policy enforcement—guarantees absolute precision.
This precision is critical because the final documents must be uploaded directly into the PPMO’s e-GP (electronic Government Procurement) system, accessible via the Bolpatra web portal. The e-GP Phase II system manages the entire lifecycle from planning to contract management, and it requires strictly formatted data to interface with banking institutions for the automated verification of these bid and performance securities. Malformed data generated by an unconstrained LLM would immediately break this digital supply chain.
The Validation Pipeline and Agentic Feedback Loops
Building deterministic LLM systems requires acknowledging that prompt engineering alone is insufficient; it must be paired with programmatic validation. The optimal architecture for legal agentic systems operates on a multi-layered verification strategy, separating structural checks from semantic evaluations to optimize latency and cost.
Layer 1: The Deterministic Floor
Every request generated by the LLM must first pass through a deterministic floor before any downstream application consumes it. This layer operates at the gateway hop and incurs virtually zero API costs, acting as a rigid filter. The deterministic floor includes:
- Schema Validation: Ensuring the output strictly matches the required JSON or Pydantic model, verifying data types, and checking that required fields are present and not hallucinated.
- Regex and Format Checks: Validating that specific legal identifiers match required patterns. For instance, ensuring that a Nepalese Permanent Account Number (PAN) conforms to tax authority standards, or that Contract Identification Codes follow the strict PPMO nomenclature (e.g., PPCR/DHM/W/NCB-12).
- Citation Validity: In Retrieval-Augmented Generation (RAG) systems drafting complex method statements, the floor checks that every generated claim contains a valid citation pointing to an actual source document, ensuring complete traceability and preventing fabricated case law.
These deterministic checks catch structural failures, format violations, and overt hallucinations in milliseconds. If a failure occurs, an automatic retry loop feeds the exact error back to the LLM, correcting the issue seamlessly without human intervention.
Layer 2: LLM-as-a-Judge for Semantic Verification
Once the output passes the deterministic floor, evaluating the subjective legal quality of the text requires semantic verification. This is achieved using the LLM-as-a-Judge pattern, where a separate model evaluates the generated output against a strict grading rubric.
Because evaluation is computationally simpler than open-ended generation, an LLM judge can effectively assess whether the drafted clause maintains a professional tone, avoids prohibited contractual terms, and addresses the specific needs of the public entity. The judge is provided with the input context, the expected outcome, and the generated output, returning a definitive pass or fail verdict along with a chain of reasoning to justify its score.
This hybrid architecture—combining Pydantic-enforced syntax at the deterministic floor with LLM-evaluated semantics at the higher level—ensures that the final generated documents strictly adhere to the rigorous standards demanded by the Public Procurement Monitoring Office.
Managing Legal Artifacts and Traceability
A truly agentic system deployed in a legal context operates under the paradigm of “Compiled AI.” The LLM functions effectively as a compiler, drawing on legal standards, source documents, and user data to build structured artifacts such as JSON schemas, templates, and validation rules.
To ensure accountability, the runtime system must maintain robust backtrace capabilities. Every generated clause, penalty rate, or qualification criterion—such as requiring a minimum average annual construction turnover calculated from the best three years within the last ten years—must be completely traceable to its originating schema and source document. By logging the exact prompt, the applied schema mask, the few-shot examples utilized, and the deterministically validated output, public entities can maintain the comprehensive audit trails required by oversight agencies and anti-corruption bodies. This traceability is non-negotiable in public procurement, where every decision must be demonstrably free from bias and anchored in statutory authority.
Conclusion
The pursuit of legal precision within artificial intelligence cannot rely on conversational fluency, vibe-coded prompting alone, or probabilistic text generation. To generate project-specific method statements, variation assessments, and custom clauses that withstand rigorous judicial and administrative scrutiny, architects must enforce absolute determinism at the infrastructure level.
By utilizing structured output mechanisms, JSON schemas, and grammar-constrained decoding, the inherent volatility of LLMs is neutralized. Models are mathematically forced to adhere to strict structural contracts, eliminating syntax errors, unauthorized field generation, and unmapped variables. When this rigid computational foundation is combined with few-shot prompting, chain-of-thought reasoning, and robust programmatic validation pipelines, these systems transcend basic text automation. They evolve into highly reliable, compiled engines capable of navigating complex regulatory frameworks—such as Nepal’s Public Procurement Act and the rigid dictates of the PPMO. This agentic approach guarantees that every generated artifact is procedurally compliant, contextually accurate, and legally unassailable, providing a scalable solution for the future of public administration and legal drafting.


