Menu

1 / 2

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

DEV Community·kol kol·6 months ago
#dmUystMC
#llm#agents#python#contractfirst#first
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.