{"slug": "rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both", "title": "RAG vs Agentic AI: A Developer's Decision Tree (With Code Examples for Both)", "summary": "A developer provides a decision tree and code examples to distinguish between RAG (Retrieval-Augmented Generation) and agentic AI architectures. RAG is recommended for answering questions from documents, while agents are suited for taking actions across multiple systems. The post includes working Python code for both approaches using LangChain and Anthropic's Claude.", "body_md": "*Two different problems wearing similar clothes. Here's how to tell them apart in thirty seconds, with working code for both.*\n\nI see this confusion in almost every project kickoff: \"We need RAG\" when the actual requirement is agentic, or \"we need an agent\" when RAG would be simpler, cheaper, and faster to ship.\n\nLet's fix that with a decision tree you can actually use, plus working code for each path.\n\n```\nDoes your system need to ANSWER QUESTIONS from documents?\n├── YES, and that's the whole job → RAG\n└── YES, but it also needs to TAKE ACTIONS across systems\n    └── → Agent that uses RAG as a tool\n\nDoes your system need to TAKE ACTIONS across multiple systems?\n├── YES, with no document retrieval needed → Plain Agent\n└── YES, and it needs grounded knowledge from documents → \n    → Agent that uses RAG as a tool\n```\n\nThe test question that resolves most confusion: **\"Does this system need to decide what to do, or does it need to find and synthesise information?\"** Finding and synthesising → RAG. Deciding and acting → agent.\n\nRAG is the right architecture when your job is grounding LLM responses in a specific document set, answering questions, summarising content, finding relevant passages.\n\n``` python\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\nfrom langchain.embeddings import HuggingFaceEmbeddings\nfrom langchain.vectorstores import Chroma\nfrom langchain.chains import RetrievalQA\nfrom langchain_anthropic import ChatAnthropic\n\n# 1. Load and chunk documents\nsplitter = RecursiveCharacterTextSplitter(\n    chunk_size=800, \n    chunk_overlap=100\n)\nchunks = splitter.split_documents(documents)\n\n# 2. Embed and store\nembeddings = HuggingFaceEmbeddings(\n    model_name=\"sentence-transformers/all-mpnet-base-v2\"\n)\nvectorstore = Chroma.from_documents(chunks, embeddings)\n\n# 3. Build the retrieval chain\nllm = ChatAnthropic(model=\"claude-sonnet-4-5\")\nqa_chain = RetrievalQA.from_chain_type(\n    llm=llm,\n    chain_type=\"stuff\",\n    retriever=vectorstore.as_retriever(search_kwargs={\"k\": 4}),\n    return_source_documents=True\n)\n\n# 4. Query\nresult = qa_chain({\"query\": \"What is our refund policy for enterprise customers?\"})\nprint(result[\"result\"])\nprint(result[\"source_documents\"])  # Always show sources\n```\n\nThis is the whole job: retrieve relevant chunks, ground the LLM's answer in them, return a response with citations. No planning loop, no tool orchestration, no multi-step decision-making. If your use case stops here, building agent infrastructure on top of this is unnecessary complexity.\n\nAn agent is right when the job is taking actions, checking systems, executing operations, making decisions that span multiple steps and there's no document knowledge base involved.\n\n``` python\nimport anthropic\n\nclient = anthropic.Anthropic()\n\ntools = [\n    {\n        \"name\": \"check_inventory\",\n        \"description\": \"Check current stock level for a SKU\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\"sku\": {\"type\": \"string\"}},\n            \"required\": [\"sku\"]\n        }\n    },\n    {\n        \"name\": \"create_purchase_order\",\n        \"description\": \"Create a PO with a supplier\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"supplier_id\": {\"type\": \"string\"},\n                \"sku\": {\"type\": \"string\"},\n                \"quantity\": {\"type\": \"integer\"}\n            },\n            \"required\": [\"supplier_id\", \"sku\", \"quantity\"]\n        }\n    }\n]\n\ndef run_inventory_agent(goal: str) -> str:\n    messages = [{\"role\": \"user\", \"content\": goal}]\n\n    for _ in range(6):\n        response = client.messages.create(\n            model=\"claude-sonnet-4-5\",\n            max_tokens=1500,\n            tools=tools,\n            messages=messages\n        )\n\n        if response.stop_reason == \"end_turn\":\n            return next(b.text for b in response.content if hasattr(b, 'text'))\n\n        messages.append({\"role\": \"assistant\", \"content\": response.content})\n        tool_results = []\n\n        for block in response.content:\n            if block.type == \"tool_use\":\n                result = execute_inventory_tool(block.name, block.input)\n                tool_results.append({\n                    \"type\": \"tool_result\",\n                    \"tool_use_id\": block.id,\n                    \"content\": result\n                })\n\n        messages.append({\"role\": \"user\", \"content\": tool_results})\n\n    return \"Reached max iterations.\"\n\nrun_inventory_agent(\n    \"Check stock for SKU-4471. If below 50 units, \"\n    \"create a PO with our primary supplier for 200 units.\"\n)\n```\n\nNo documents involved. The agent checks inventory, reasons about the threshold, and conditionally creates a purchase order. This is pure action orchestration.\n\nThis is where most real enterprise systems actually land: an agent that needs to take actions, and one of the things it needs to do along the way is look something up in a document knowledge base.\n\n``` php\nimport anthropic\n\nclient = anthropic.Anthropic()\n\ndef rag_lookup(query: str) -> str:\n    \"\"\"RAG retrieval wrapped as a tool the agent can call.\"\"\"\n    result = qa_chain({\"query\": query})  # the RAG chain from Path 1\n    return json.dumps({\n        \"answer\": result[\"result\"],\n        \"sources\": [doc.metadata.get(\"source\") for doc in result[\"source_documents\"]]\n    })\n\ntools = [\n    {\n        \"name\": \"search_policy_documents\",\n        \"description\": \"Search company policy documents for relevant information\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\"query\": {\"type\": \"string\"}},\n            \"required\": [\"query\"]\n        }\n    },\n    {\n        \"name\": \"issue_refund\",\n        \"description\": \"Process a refund for a customer order\",\n        \"input_schema\": {\n            \"type\": \"object\",\n            \"properties\": {\n                \"order_id\": {\"type\": \"string\"},\n                \"amount\": {\"type\": \"number\"}\n            },\n            \"required\": [\"order_id\", \"amount\"]\n        }\n    }\n]\n\ndef execute_tool(name: str, input_data: dict) -> str:\n    if name == \"search_policy_documents\":\n        return rag_lookup(input_data[\"query\"])\n    elif name == \"issue_refund\":\n        return process_refund(input_data[\"order_id\"], input_data[\"amount\"])\n\ndef run_refund_agent(customer_request: str) -> str:\n    messages = [{\"role\": \"user\", \"content\": customer_request}]\n\n    for _ in range(6):\n        response = client.messages.create(\n            model=\"claude-sonnet-4-5\",\n            max_tokens=1500,\n            tools=tools,\n            messages=messages\n        )\n\n        if response.stop_reason == \"end_turn\":\n            return next(b.text for b in response.content if hasattr(b, 'text'))\n\n        messages.append({\"role\": \"assistant\", \"content\": response.content})\n        tool_results = [\n            {\"type\": \"tool_result\", \"tool_use_id\": block.id,\n             \"content\": execute_tool(block.name, block.input)}\n            for block in response.content if block.type == \"tool_use\"\n        ]\n        messages.append({\"role\": \"user\", \"content\": tool_results})\n\n    return \"Reached max iterations.\"\n\nrun_refund_agent(\n    \"Customer wants a refund on order #8821 for $340. \"\n    \"Check our refund policy first to see if this qualifies.\"\n)\n```\n\nThe agent decides to call `search_policy_documents`\n\nto check eligibility before deciding whether to call `issue_refund`\n\n. The RAG system is doing exactly what it's good at, grounded retrieval, but it's a tool in service of the agent's broader decision-making, not the entire system.\n\nRAG-only systems are cheaper to build and run. Single retrieval call, single generation call, predictable latency, easier to evaluate (you can measure retrieval precision and answer accuracy independently).\n\nAgentic systems are more expensive and harder to debug. Multiple LLM calls per task, unpredictable latency (depends how many iterations the agent takes), harder to evaluate because failure can happen at the planning stage or the execution stage. They're also the only option when the task genuinely requires multi-step action across systems.\n\nThe mistake we see most often: teams building agentic infrastructure for what's fundamentally a question-answering problem, paying the complexity cost for capability they don't need.\n\nThe full ** RAG vs agentic AI** comparison covers the cost modelling, latency benchmarks, and evaluation methodology differences in more depth.\n\nOnce you've picked your architecture, the next question is build vs buy, do you build this RAG pipeline or agent loop yourself, or do you use a managed platform? The answer depends on your timeline, your team's capacity, and how differentiated your specific use case actually is. We wrote the framework with cost models, time estimates, and decision criteria for exactly this question, worth reading ** before you commit engineering time** to either path.\n\n*Published by Dextra Labs | AI Consulting & Enterprise Agent Development*", "url": "https://wpnews.pro/news/rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both", "canonical_source": "https://dev.to/dextralabs/rag-vs-agentic-ai-a-developers-decision-tree-with-code-examples-for-both-2nh3", "published_at": "2026-06-24 20:57:33+00:00", "updated_at": "2026-06-24 21:13:04.253014+00:00", "lang": "en", "topics": ["large-language-models", "generative-ai", "ai-agents", "developer-tools", "natural-language-processing"], "entities": ["LangChain", "HuggingFace", "Chroma", "Anthropic", "Claude"], "alternates": {"html": "https://wpnews.pro/news/rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both", "markdown": "https://wpnews.pro/news/rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both.md", "text": "https://wpnews.pro/news/rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both.txt", "jsonld": "https://wpnews.pro/news/rag-vs-agentic-ai-a-developer-s-decision-tree-with-code-examples-for-both.jsonld"}}