Contract-First vs Assertion-First: Which Pattern Makes Your LLM Agents More Reliable? When building AI agents that interact with APIs, databases, or external systems, you'll quickly hit a reliability wall. LLMs are brilliant but non-deterministic. Two patterns have emerged to handle this: Contract-First Approach Define the expected output schema upfront, then validate every LLM response against it: from pydantic import BaseModel class AgentOutput ( BaseModel ): action : str parameters : dict confidence : float # LLM response must conform to this schema output = AgentOutput . model_validate ( llm_response ) Enter fullscreen mode Exit fullscreen mode Pros: Fail fast, clear interface, IDE autocomplete support Cons: Rigid, can reject creative but valid outputs Assertion-First Approach Let the LLM generate freely, then assert on the results: result = llm . generate () assert result . get ( " action " ) in [ " read " , " write " , " delete " ] assert isinstance ( result .…