How to Integrate CrewAI for lending with FastAPI for RAG
Combining CrewAI for lending with FastAPI gives you a clean way to expose a lending-specific agent workflow behind an API. The practical win is simple: FastAPI handles request/response, while CrewAI orchestrates retrieval, reasoning, and task execution for RAG-driven lending use cases like loan eligibility checks, policy Q&A, and document-assisted underwriting.
Prerequisites
- •Python 3.10+
- •
fastapi - •
uvicorn - •
crewai - •A vector store or retriever for RAG, such as:
- •Chroma
- •FAISS
- •Pinecone
- •Access to your LLM provider configured in environment variables
- •Lending knowledge base documents:
- •underwriting policy PDFs
- •product sheets
- •compliance docs
- •FAQs
- •Basic familiarity with:
- •FastAPI route definitions
- •CrewAI
Agent,Task, andCrew - •retrieval pipelines for RAG
Integration Steps
- •Set up the FastAPI app and load your lending knowledge base.
You want the API layer to stay thin. The app should accept a user question, retrieve relevant lending context, and hand that context to CrewAI.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Lending RAG API")
class LendingQuery(BaseModel):
question: str
customer_profile: dict | None = None
For RAG, keep your retriever separate from the API route. That makes it easier to swap Chroma for Pinecone later.
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
vectorstore = Chroma(
collection_name="lending_docs",
embedding_function=embeddings,
persist_directory="./chroma_db"
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
- •Define a CrewAI agent specialized for lending decisions.
CrewAI works best when the agent has a narrow role. For lending, make the agent focus on policy interpretation and customer-facing answers grounded in retrieved context.
from crewai import Agent
lending_agent = Agent(
role="Lending Policy Analyst",
goal="Answer lending questions using retrieved policy context and customer profile data",
backstory=(
"You are a lending operations analyst who explains loan policy, "
"eligibility rules, and document requirements using only provided context."
),
verbose=True,
allow_delegation=False,
)
If you need stricter behavior, add guardrails in the prompt and keep the agent from inventing facts outside the retrieved chunks.
- •Create a task that injects retrieved context into the agent input.
This is where RAG meets CrewAI. Retrieve documents first, then pass those chunks into the task description so the model has grounded context.
from crewai import Task
def build_context(question: str) -> str:
docs = retriever.get_relevant_documents(question)
return "\n\n".join([f"- {doc.page_content}" for doc in docs])
def create_lending_task(question: str, customer_profile: dict | None):
context = build_context(question)
return Task(
description=(
f"Answer the user's lending question using only this context:\n\n{context}\n\n"
f"Customer profile: {customer_profile or {}}\n\n"
f"Question: {question}"
),
expected_output="A concise lending answer with policy references and next steps.",
agent=lending_agent,
)
That pattern is production-friendly because retrieval stays deterministic and observable. You can log retrieved chunk IDs before the model ever runs.
- •Execute the CrewAI workflow from a FastAPI endpoint.
Now wire everything together in one route. The endpoint receives input, builds a task, runs the crew, and returns the result.
from crewai import Crew
@app.post("/lending/rag")
def answer_lending_query(payload: LendingQuery):
task = create_lending_task(payload.question, payload.customer_profile)
crew = Crew(
agents=[lending_agent],
tasks=[task],
verbose=True,
)
result = crew.kickoff()
return {
"question": payload.question,
"answer": str(result),
}
This is the core integration point. FastAPI exposes the interface; CrewAI handles orchestration; your retriever supplies grounded context.
- •Run the service and keep latency under control.
For local testing:
uvicorn main:app --reload --port 8000
In production, separate retrieval latency from generation latency by caching embeddings and tuning top-k retrieval. If your documents are large, chunk them properly before indexing so you don’t overload the prompt window.
Testing the Integration
Use curl or Python requests to verify end-to-end behavior.
import requests
response = requests.post(
"http://127.0.0.1:8000/lending/rag",
json={
"question": "What documents are required for an unsecured personal loan?",
"customer_profile": {
"employment_status": "full-time",
"credit_score": 710,
"income": 65000
}
},
)
print(response.status_code)
print(response.json())
Expected output:
{
"question": "What documents are required for an unsecured personal loan?",
"answer": "Based on the provided policy context, applicants typically need proof of income, government ID, recent bank statements..."
}
If you get empty or vague answers, inspect three things first:
- •Whether retrieval is returning relevant chunks
- •Whether your prompt restricts answers to provided context
- •Whether your source docs are actually indexed in the vector store
Real-World Use Cases
- •
Loan eligibility assistant
Build an internal API that checks applicant profiles against policy docs and returns a grounded eligibility summary. - •
Underwriting copilot
Let analysts ask questions like “What exceptions apply to self-employed borrowers?” and get answers pulled from underwriting manuals and product rules. - •
Customer support RAG bot
Expose a lending FAQ endpoint that answers questions about rates, required documents, repayment terms, and application status policies without hardcoding responses.
The pattern is straightforward: FastAPI gives you an API surface, retrieval gives you grounding, and CrewAI gives you controlled agent orchestration. For lending teams, that combination is usually enough to move from static FAQ systems to something that can actually reason over policy documents without turning into a black box.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
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