What I Learned While Developing AI Agents
Developing AI Agents is fundamentally different from traditional deterministic backend engineering. Transitioning from 20 years of enterprise .NET systems into building Generative AI applications forced me to rethink how software handles state, context, and failure. Here are the core architectural lessons from building production RAG (Retrieval-Augmented Generation) and agentic workflows.
1. Context Is King—Until Signal Drowns in Noise
A common early mistake is stuffing everything into the prompt just because modern models like Gemini 2.5 offer million-token context windows. Dumping dozens of marginally related documents degrades reasoning precision—a well-documented failure mode known as the "Lost in the Middle" effect, alongside unnecessary latency and token cost.
- Lesson: Pair hybrid retrieval (exact SQL/lexical boundaries + dense vector similarity) with strict top-k pruning and re-ranking. Retrieve less, verify relevance, and pass only high-signal context.
2. Tool Schemas Are Executable Contracts
When using Function / Tool Calling, the model never inspects your backend source code; its entire understanding comes from the tool's name, parameter JSON Schema, and docstring.
"In agentic systems, a tool's schema and parameter descriptions are just as critical as the implementation behind them."
If parameter semantics, units, or edge cases are ambiguous, the model will invent plausible-looking arguments. Define strict enum sets, explicit formats, and unambiguous preconditions on every tool boundary.
3. Semantic Validation Over Blind try/catch
In classical backend services, an invalid state usually throws an exception caught by a try/catch block. Probabilistic models fail differently: they can return an HTTP 200 OK containing a polite refusal, an ungrounded guess, or low-confidence filler without throwing a runtime exception.
True Semantic Error Handling validates the structured payload and grounding confidence before accepting the turn:
from pydantic import BaseModel, Field
class GroundedAnswer(BaseModel):
answer: str
cited_chunk_ids: list[str]
confidence: float = Field(ge=0.0, le=1.0)
insufficient_context: bool
def execute_grounded_agent(query: str) -> GroundedAnswer:
# Primary pass: fast reasoning with Gemini 2.5 Flash
result: GroundedAnswer = flash_agent.run_structured(query, schema=GroundedAnswer)
# Semantic check: HTTP 200 is not enough—verify grounding & confidence
if result.insufficient_context or result.confidence < 0.75 or not result.cited_chunk_ids:
# Escalate ambiguous queries to deeper reasoning (Gemini 2.5 Pro)
return pro_agent.run_structured(query, schema=GroundedAnswer)
return result
4. Evals Are the Unit Tests of AI Systems
Unit tests verify deterministic code paths (2 + 2 == 4), whereas AI pipelines require Automated Evals. Maintaining a golden dataset of real queries—asserting both deterministic tool/schema expectations and LLM-as-a-judge faithfulness rubrics—is the only reliable way to catch regressions when upgrading models or modifying retrieval logic.
Conclusion
Moving from purely deterministic C# pipelines to hybrid AI systems requires a disciplined shift: we no longer just write step-by-step instructions; we engineer contracts, guardrails, and verification loops around probabilistic reasoning engines.