How to Integrate CrewAI for insurance with FastAPI for RAG

By Cyprian AaronsUpdated 2026-04-22
crewai-for-insurancefastapirag

Combining CrewAI for insurance with FastAPI gives you a clean way to expose agent-driven RAG workflows as production APIs. The pattern is simple: FastAPI handles request/response, authentication, and service boundaries, while CrewAI for insurance coordinates the retrieval and reasoning steps across policy docs, claims notes, underwriting rules, and customer history.

Prerequisites

  • Python 3.10+
  • fastapi
  • uvicorn
  • crewai
  • crewai-tools or your preferred retrieval tool wrapper
  • A vector store for RAG, such as:
    • Chroma
    • Pinecone
    • FAISS
  • Access to your LLM provider key in environment variables
  • A document corpus for insurance knowledge:
    • policy wordings
    • claims manuals
    • underwriting guidelines
    • FAQ and servicing docs

Install the core packages:

pip install fastapi uvicorn crewai crewai-tools pydantic python-dotenv

Integration Steps

  1. Set up your insurance knowledge source

    For RAG, you need a retriever that can return relevant chunks before the agent reasons over them. In production, keep ingestion separate from the API layer.

from crewai_tools import PDFSearchTool

policy_search = PDFSearchTool(
    pdf="data/insurance_policy.pdf"
)

claims_search = PDFSearchTool(
    pdf="data/claims_handbook.pdf"
)

If you already have embeddings in a vector database, swap in your retriever tool there. The important part is that CrewAI can call it as a tool during task execution.

  1. Define the insurance agents and tasks

    CrewAI organizes work around agents and tasks. For an insurance RAG workflow, one agent can retrieve policy evidence while another drafts the final answer.

from crewai import Agent, Task, Crew, Process

retrieval_agent = Agent(
    role="Insurance Knowledge Retriever",
    goal="Find relevant policy and claims evidence for customer questions",
    backstory="You know how to locate exact clauses in insurance documents.",
    tools=[policy_search, claims_search],
    verbose=True,
)

answer_agent = Agent(
    role="Insurance Response Writer",
    goal="Generate a grounded response using retrieved evidence only",
    backstory="You write concise, compliant answers for insurance operations.",
    verbose=True,
)

retrieve_task = Task(
    description=(
        "Find the most relevant clauses for this question: "
        "{question}. Return citations and short excerpts."
    ),
    expected_output="Relevant policy or claims excerpts with source references.",
    agent=retrieval_agent,
)

answer_task = Task(
    description=(
        "Using the retrieved evidence, answer the question: {question}. "
        "If evidence is missing, say so clearly."
    ),
    expected_output="A grounded insurance answer with citations.",
    agent=answer_agent,
)
  1. Wrap the CrewAI workflow behind a FastAPI endpoint

    FastAPI becomes the entry point for your RAG system. Keep the API thin: validate input, call the crew, return structured output.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="Insurance RAG API")

class QueryRequest(BaseModel):
    question: str

@app.post("/rag/insurance")
def ask_insurance_question(payload: QueryRequest):
    crew = Crew(
        agents=[retrieval_agent, answer_agent],
        tasks=[retrieve_task, answer_task],
        process=Process.sequential,
        verbose=True,
    )

    result = crew.kickoff(inputs={"question": payload.question})
    return {"answer": str(result)}

This is the core integration point. FastAPI owns HTTP concerns; CrewAI owns orchestration.

  1. Add production-safe response shaping

    Don’t return raw agent text if you need consistent downstream consumption. Wrap the output into a stable schema so frontend apps or internal systems can parse it safely.

from typing import List

class QueryResponse(BaseModel):
    answer: str
    sources: List[str] = []

@app.post("/rag/insurance", response_model=QueryResponse)
def ask_insurance_question(payload: QueryRequest):
    crew = Crew(
        agents=[retrieval_agent, answer_agent],
        tasks=[retrieve_task, answer_task],
        process=Process.sequential,
    )

    result = crew.kickoff(inputs={"question": payload.question})

    return QueryResponse(
        answer=str(result),
        sources=["policy.pdf", "claims_handbook.pdf"]
    )

In a real implementation, extract actual citations from your retriever output instead of hardcoding source names.

  1. Run the API and wire environment config

    Load secrets from environment variables and start Uvicorn as your app server.

import os
from dotenv import load_dotenv

load_dotenv()

# Example: make sure your model provider key is available
llm_api_key = os.getenv("OPENAI_API_KEY")
if not llm_api_key:
    raise RuntimeError("OPENAI_API_KEY is required")

# Start with:
# uvicorn main:app --reload --port 8000

Testing the Integration

Use curl or Python requests to verify the endpoint returns a grounded response.

import requests

response = requests.post(
    "http://localhost:8000/rag/insurance",
    json={"question": "Does this policy cover water damage from burst pipes?"}
)

print(response.status_code)
print(response.json())

Expected output:

{
  "answer": "Coverage may apply if the damage was sudden and accidental... [citation]",
  "sources": ["policy.pdf", "claims_handbook.pdf"]
}

If you get a 200 but empty or generic answers, check these first:

  • The retriever tool is actually connected to relevant documents.
  • The task prompt forces evidence-based answers.
  • Your API key and model settings are loaded correctly.
  • The agent has access to tools in tools=[...].

Real-World Use Cases

  • Claims triage assistant
    Route incoming FNOL questions through an agent that checks policy terms, exclusions, and claim handling rules before assigning next actions.

  • Underwriting support bot
    Let underwriters ask natural-language questions about appetite rules, endorsements, and risk criteria with citations back to internal manuals.

  • Customer servicing API
    Expose policy Q&A over FastAPI so call center tools can fetch grounded answers about deductibles, waiting periods, or coverage limits without manual lookup.


Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides