How to Integrate CrewAI for banking with FastAPI for AI agents
Combining CrewAI for banking with FastAPI gives you a clean way to expose multi-agent workflows as production APIs. That matters when you want an agent system that can triage customer requests, summarize account activity, flag suspicious patterns, or route tasks to specialized banking agents behind a standard HTTP interface.
The pattern is simple: CrewAI handles orchestration between agents and tasks, while FastAPI exposes those workflows to your internal systems, portals, or downstream services.
Prerequisites
- •Python 3.10+
- •A working FastAPI project
- •
crewaiinstalled and configured for your banking use case - •
uvicornfor local API serving - •Access credentials or environment variables required by your CrewAI setup
- •Basic familiarity with Pydantic models and async HTTP endpoints
Install the packages:
pip install fastapi uvicorn crewai pydantic
Integration Steps
- •Define your banking agents and task flow
Start by modeling the work you want the agent system to do. In banking, keep agents narrow: one for customer support, one for compliance review, one for transaction analysis.
from crewai import Agent, Task, Crew, Process
support_agent = Agent(
role="Banking Support Agent",
goal="Resolve customer banking queries accurately",
backstory="Handles account questions, card issues, and service requests.",
verbose=True,
)
compliance_agent = Agent(
role="Compliance Reviewer",
goal="Check responses for policy and regulatory issues",
backstory="Reviews outputs for banking policy alignment.",
verbose=True,
)
support_task = Task(
description="Answer the customer's question about a recent card charge.",
expected_output="A concise customer-ready explanation.",
agent=support_agent,
)
compliance_task = Task(
description="Review the support response for compliance risk.",
expected_output="Approved response or flagged issue.",
agent=compliance_agent,
)
- •Wrap the CrewAI workflow in a reusable function
Your API endpoint should not build the crew inline every time. Put the orchestration in a function so it is testable and easy to extend.
def run_banking_crew(customer_message: str) -> str:
support_agent = Agent(
role="Banking Support Agent",
goal="Resolve customer banking queries accurately",
backstory="Handles account questions, card issues, and service requests.",
verbose=True,
)
compliance_agent = Agent(
role="Compliance Reviewer",
goal="Check responses for policy and regulatory issues",
backstory="Reviews outputs for banking policy alignment.",
verbose=True,
)
support_task = Task(
description=f"Respond to this customer message: {customer_message}",
expected_output="A clear support response.",
agent=support_agent,
)
compliance_task = Task(
description="Review the support response for compliance risk.",
expected_output="A compliant final response.",
agent=compliance_agent,
context=[support_task],
)
crew = Crew(
agents=[support_agent, compliance_agent],
tasks=[support_task, compliance_task],
process=Process.sequential,
verbose=True,
)
result = crew.kickoff()
return str(result)
- •Expose the workflow through FastAPI
Use a request model so your endpoint accepts structured input. This keeps the contract stable for frontend apps and internal consumers.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Banking AI Agent API")
class BankingRequest(BaseModel):
customer_message: str
class BankingResponse(BaseModel):
result: str
@app.post("/agents/banking/support", response_model=BankingResponse)
def banking_support(request: BankingRequest):
result = run_banking_crew(request.customer_message)
return BankingResponse(result=result)
- •Add async-safe execution if the crew is slow
CrewAI workflows are often CPU/network bound because they call models and tools. In FastAPI, offload blocking work so your server stays responsive.
from fastapi.concurrency import run_in_threadpool
@app.post("/agents/banking/support/async", response_model=BankingResponse)
async def banking_support_async(request: BankingRequest):
result = await run_in_threadpool(run_banking_crew, request.customer_message)
return BankingResponse(result=result)
- •Run the API and connect it to other systems
Serve the app with Uvicorn and call it from another service, a workflow engine, or your bank’s internal portal.
uvicorn main:app --reload --host 0.0.0.0 --port 8000
If you need to call it from Python:
import requests
payload = {"customer_message": "Why was my debit card declined yesterday?"}
response = requests.post("http://localhost:8000/agents/banking/support", json=payload)
print(response.status_code)
print(response.json())
Testing the Integration
Hit the endpoint directly with FastAPI’s test client. This verifies that routing, request parsing, and CrewAI execution are wired correctly.
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_banking_support():
response = client.post(
"/agents/banking/support",
json={"customer_message": "I see a duplicate charge on my account."},
)
assert response.status_code == 200
assert "result" in response.json()
print(response.json())
test_banking_support()
Expected output:
{
"result": "..."
}
If you want more confidence, assert on business behavior too:
- •The response is not empty
- •The compliance step ran
- •The output does not contain disallowed phrasing or unsupported claims
Real-World Use Cases
- •Customer service triage: Route balance questions, card disputes, loan FAQs, and escalation handling through specialized agents exposed via FastAPI.
- •Compliance-assisted drafting: Generate customer-facing responses and send them through a review agent before returning them to users.
- •Ops automation: Build internal endpoints for fraud review summaries, case routing, or account activity explanations consumed by back-office tools.
The main win here is control. CrewAI gives you structured multi-agent reasoning; FastAPI gives you an HTTP boundary that fits into real banking systems without forcing teams to integrate directly with agent internals.
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