Building Production-Ready AI Agents with LangChain and Claude
A practical guide to building AI agents that actually work outside of demos — covering tool selection, memory, error recovery, and deployment patterns we've validated in production.
Two approaches, very different trade-offs. RAG is faster, cheaper, and keeps your data current. Fine-tuning gives you style and behaviour control. Here's when each wins — and when to use both.
Rahul Sharma
Head of AI Engineering
RAG (Retrieval-Augmented Generation) augments a model's knowledge at inference time by retrieving relevant documents and including them in the prompt. Fine-tuning changes the model's weights — permanently baking knowledge or behaviour into the model itself.
The distinction matters enormously for enterprise use cases. RAG is a retrieval problem. Fine-tuning is a training problem. They solve different things, and the worst deployments we've seen tried to use one where the other was needed.
Use RAG when your data changes frequently, when you need citations, or when your knowledge base is too large to fit in a context window. A legal tech company with 200,000 contracts that updates daily should use RAG — not fine-tuning. The data would be stale before you finished training.
RAG also wins when you need auditable responses. Every answer can be traced back to the documents it was retrieved from. For regulated industries like healthcare and fintech, this auditability is often a compliance requirement, not a nice-to-have — it's the core of our RAG development work with regulated clients.
Fine-tuning wins when you need consistent output format, tone, or behaviour — things that RAG can't reliably enforce through prompting alone. A company that needs every customer communication to follow a specific brand voice, use internal terminology, and avoid certain topics will get better results from fine-tuning than from increasingly complex system prompts.
Fine-tuning also wins for latency-sensitive applications. A fine-tuned model doesn't need a retrieval step. For high-frequency, low-latency use cases (real-time classification, autocomplete), the additional round-trip of RAG is a real cost.
A working RAG prototype is easy. A RAG system that survives real user queries takes more care. Chunk size matters more than most teams expect — chunks of 200-400 tokens with 10-15% overlap outperform naive fixed-size splitting on legal and technical documents, where clause boundaries carry meaning. We default to semantic chunking, splitting on section headers and paragraph boundaries, over fixed-token chunking for any document longer than a few pages.
Embedding model choice is the second lever. OpenAI's text-embedding-3-large gives strong general-purpose retrieval, but domain-specific embeddings fine-tuned on your own document corpus improve recall by 8-15% on internal benchmarks for specialized domains like insurance policy language or clinical notes. Hybrid search — combining dense vector search with BM25 keyword search and reranking the merged results with a cross-encoder — consistently outperforms pure vector search, especially for queries containing exact terms like case numbers, product SKUs, or regulation codes that embeddings alone won't rank correctly.
RAG's costs are operational: vector database hosting, embedding API calls, and the additional tokens in every prompt. Fine-tuning's costs are upfront: training compute, data preparation, and evaluation. For most enterprise use cases with dynamic data, RAG's operational cost is lower than the ongoing fine-tuning cycles needed to keep the model current.
We ran the numbers on a 50-seat legal tech deployment: RAG cost $2,800/month ongoing. Equivalent fine-tuning (monthly retraining to stay current) cost $18,000/month in compute plus engineering time. RAG won easily.
The most common RAG failure isn't the retrieval model — it's the chunking strategy. Teams split documents by fixed character count, severing sentences and tables mid-way, then wonder why retrieved context reads like nonsense. Fix: chunk on structural boundaries the document already provides — headings, list items, table rows — even if it means variable chunk sizes.
The second common failure is skipping reranking. Vector similarity search alone returns semantically similar chunks, not necessarily the most relevant ones for the exact question asked. Adding a reranking step, Cohere Rerank or a self-hosted cross-encoder, after initial retrieval, even just reordering the top 20 down to the top 5, measurably improves answer accuracy — we've seen 10-20% improvements in our own evaluation sets.
On the fine-tuning side, the most damaging mistake is training on too small or too narrow a dataset. Fine-tuning on 200 examples of one document type risks catastrophic forgetting: the model gets very good at that one format and measurably worse at everything else it used to handle. We require a minimum of 500-1,000 diverse, high-quality examples before recommending fine-tuning over prompt engineering alone — anything less and the ROI usually isn't there yet.
The most capable enterprise AI systems we've built use both: fine-tuning for behaviour, format, and domain vocabulary — and RAG for current, dynamic knowledge. Fine-tune a base model on your industry's language and your company's communication style. Then use RAG to inject current facts, documents, and data at inference time. Our LLM development team designs this split on a case-by-case basis for enterprise clients.
Before recommending RAG, fine-tuning, or both, we run a small evaluation: 30-50 real questions from the client's actual users, scored against a baseline RAG pipeline and, where fine-tuning is on the table, a LoRA fine-tune on a sample of their data. We measure answer accuracy, hallucination rate, and latency side by side. The evaluation itself takes 1-2 weeks and consistently changes the initial assumption in about a third of engagements — clients often start convinced they need fine-tuning and end up shipping a RAG-only system, or vice versa, once the numbers are in front of them.
Start with these questions: Does your data change more than monthly? → RAG. Do you need citations? → RAG. Is output format or brand voice the problem? → Fine-tune. Is the model failing on industry-specific terms or concepts that won't change? → Fine-tune. If both apply, start with RAG and layer fine-tuning once you have production data to train on.
Tags
Continue Reading
A practical guide to building AI agents that actually work outside of demos — covering tool selection, memory, error recovery, and deployment patterns we've validated in production.
Most prompt engineering guides are written for demos. Production prompts face hallucinations, adversarial inputs, context length limits, and latency budgets. Here's what actually holds up.
Moving computer vision models from Jupyter notebooks to production serving infrastructure requires solving latency, throughput, model versioning, and data drift challenges. Here's our battle-tested playbook.
From AI agents to mobile apps — iMOBDEV builds what your business needs.