Menu

Post image 1
Post image 2
1 / 2
0

Contract-First vs Assertion-First: Which Pattern Makes Your LLM Agents More Reliable?

DEV Community·kol kol·17 days ago
#dmUystMC
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More