How to Integrate CrewAI for lending with FastAPI for production AI

By Cyprian AaronsUpdated 2026-04-22
crewai-for-lendingfastapiproduction-ai

CrewAI for lending gives you the agent orchestration layer for credit workflows: document review, borrower Q&A, policy checks, and decision support. FastAPI gives you the HTTP surface to expose those workflows as production endpoints, so your lending agents can be called from underwriting systems, portals, or internal ops tools.

The combination is useful when you need a clean API boundary around multi-step AI work. Instead of wiring agent logic directly into your app, you wrap it in FastAPI and keep auth, validation, retries, and observability in one place.

Prerequisites

  • Python 3.10+
  • A FastAPI project set up with uvicorn
  • CrewAI installed and configured for your lending workflow
  • An LLM provider key set in environment variables
  • Basic Pydantic models for request/response validation
  • A clear lending use case:
    • pre-qualification
    • document summarization
    • policy-based loan review
    • borrower support

Install the dependencies:

pip install fastapi uvicorn crewai pydantic python-dotenv

Integration Steps

  1. Define your lending agents and task

    Start by modeling the lending workflow as a CrewAI crew. In production, keep the task narrow: one crew per business capability.

from crewai import Agent, Task, Crew, Process

loan_analyst = Agent(
    role="Loan Analyst",
    goal="Review borrower data and summarize lending risk",
    backstory="You analyze credit applications using lending policy rules and document context."
)

risk_reviewer = Agent(
    role="Risk Reviewer",
    goal="Validate the analyst summary against lending policy",
    backstory="You check for missing fields, policy violations, and approval blockers."
)

review_task = Task(
    description=(
        "Review this loan application data and produce a concise underwriting summary "
        "with risk flags, missing information, and next actions."
    ),
    expected_output="A structured lending review with risk assessment and recommendations.",
    agent=loan_analyst,
)

crew = Crew(
    agents=[loan_analyst, risk_reviewer],
    tasks=[review_task],
    process=Process.sequential,
)
  1. Wrap the crew in a service function

    Don’t call the crew directly from the route handler. Put it behind a service layer so you can add retries, logging, timeouts, or queueing later.

def run_lending_review(application: dict) -> str:
    context = f"""
    Borrower Name: {application['borrower_name']}
    Loan Amount: {application['loan_amount']}
    Income: {application['income']}
    Credit Score: {application['credit_score']}
    Purpose: {application['purpose']}
    Notes: {application.get('notes', '')}
    """

    # CrewAI executes the task using the configured agents.
    result = crew.kickoff(inputs={"application_context": context})
    return str(result)
  1. Expose the workflow through FastAPI

    Use Pydantic models for request validation and response shaping. This keeps your API contract stable even if the internal prompt changes.

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

app = FastAPI(title="Lending AI API")

class LoanApplicationRequest(BaseModel):
    borrower_name: str = Field(..., min_length=2)
    loan_amount: float = Field(..., gt=0)
    income: float = Field(..., gt=0)
    credit_score: int = Field(..., ge=300, le=850)
    purpose: str
    notes: str | None = None

class LoanApplicationResponse(BaseModel):
    status: str
    review: str

@app.post("/lending/review", response_model=LoanApplicationResponse)
def review_application(payload: LoanApplicationRequest):
    try:
        review_text = run_lending_review(payload.model_dump())
        return LoanApplicationResponse(status="ok", review=review_text)
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))
  1. Add async-safe execution for production traffic

    CrewAI runs blocking work. In FastAPI endpoints that serve concurrent traffic, move that blocking call off the event loop with run_in_threadpool.

from fastapi.concurrency import run_in_threadpool

@app.post("/lending/review-async", response_model=LoanApplicationResponse)
async def review_application_async(payload: LoanApplicationRequest):
    try:
        review_text = await run_in_threadpool(run_lending_review, payload.model_dump())
        return LoanApplicationResponse(status="ok", review=review_text)
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))
  1. Run the API locally

    Keep startup simple while you validate prompts and response shape.

# save as main.py
# then run:
# uvicorn main:app --reload --host 0.0.0.0 --port 8000

Testing the Integration

Send a real request to verify both FastAPI validation and CrewAI execution are wired correctly.

import requests

payload = {
    "borrower_name": "Amina Patel",
    "loan_amount": 25000,
    "income": 72000,
    "credit_score": 710,
    "purpose": "Vehicle purchase",
}

response = requests.post("http://localhost:8000/lending/review", json=payload)
print(response.status_code)
print(response.json())

Expected output:

200
{
  "status": "ok",
  "review": "..."
}

If you want a quick sanity check before hitting the agent flow:

  • 200 means FastAPI accepted the payload and CrewAI returned text.
  • 422 means your request failed schema validation.
  • 500 usually means an LLM config issue or an exception inside the crew execution path.

Real-World Use Cases

  • Pre-underwriting triage

    • Accept applicant data from a portal.
    • Use CrewAI to summarize risk factors and missing fields.
    • Return a structured recommendation to an underwriting queue.
  • Document intake assistant

    • Expose an endpoint that ingests bank statements or payslips.
    • Have agents extract key values and flag inconsistencies.
    • Feed results into downstream decisioning systems.
  • Borrower support automation

    • Build an API for answering lending questions about eligibility, required docs, or application status.
    • Route complex cases to human staff with an AI-generated summary attached.

This pattern keeps your AI logic isolated from your web layer. That matters in production because it makes it easier to version prompts, test agent behavior, enforce auth at the API boundary, and swap orchestration internals without breaking clients.


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