How to Integrate CrewAI for investment banking with FastAPI for RAG
Combining CrewAI for investment banking with FastAPI gives you a clean way to expose agentic RAG workflows as production APIs. The pattern is straightforward: CrewAI handles orchestration across specialized finance agents, while FastAPI wraps retrieval and execution behind HTTP endpoints your internal tools can call.
Prerequisites
- •Python 3.10+
- •A working FastAPI project
- •CrewAI installed and configured
- •An LLM provider key set in your environment
- •A vector store for RAG, such as:
- •FAISS
- •Chroma
- •Pinecone
- •Investment banking source data ready for indexing:
- •pitch decks
- •CIMs
- •earnings transcripts
- •market research notes
- •
uvicornfor running the API locally
Install the core packages:
pip install fastapi uvicorn crewai crewai-tools langchain-openai faiss-cpu python-dotenv
Integration Steps
- •Set up your FastAPI app and load configuration.
You want the API layer to stay thin. Keep secrets in environment variables and initialize your model client once at startup.
# app/main.py
import os
from dotenv import load_dotenv
from fastapi import FastAPI
load_dotenv()
app = FastAPI(title="IB RAG Agent API")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
- •Build a retrieval function for investment banking documents.
For RAG, the agent should not guess from memory. It should retrieve relevant chunks from your indexed corpus and pass them into the CrewAI workflow.
# app/retrieval.py
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
def load_vectorstore(index_path: str = "./data/faiss_index"):
embeddings = OpenAIEmbeddings()
return FAISS.load_local(index_path, embeddings, allow_dangerous_deserialization=True)
def retrieve_context(query: str, k: int = 4) -> str:
vs = load_vectorstore()
docs = vs.similarity_search(query, k=k)
return "\n\n".join([doc.page_content for doc in docs])
- •Define CrewAI agents and a task that uses retrieved context.
For investment banking, keep roles narrow. One agent can summarize deal context, another can produce an analyst-style response, and a third can validate output against retrieved evidence.
# app/crew.py
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
deal_analyst = Agent(
role="Investment Banking Analyst",
goal="Answer questions using only provided deal and market context",
backstory="You support M&A and capital markets teams with precise, source-grounded analysis.",
llm=llm,
verbose=True,
)
reviewer = Agent(
role="Senior Banker Reviewer",
goal="Check the answer for factual accuracy and banking tone",
backstory="You review outputs before they are shared with clients or internal teams.",
llm=llm,
verbose=True,
)
def run_ib_crew(question: str, context: str) -> str:
task = Task(
description=(
f"Answer this investment banking question:\n{question}\n\n"
f"Use only this retrieved context:\n{context}"
),
expected_output="A concise banking answer with no unsupported claims.",
agent=deal_analyst,
)
review_task = Task(
description="Review the analyst answer for accuracy and clarity.",
expected_output="A validated final answer.",
agent=reviewer,
context=[task],
)
crew = Crew(
agents=[deal_analyst, reviewer],
tasks=[task, review_task],
process=Process.sequential,
verbose=True,
)
result = crew.kickoff()
return str(result)
- •Expose a FastAPI endpoint that connects retrieval to CrewAI execution.
This is the integration point. The endpoint receives a question, pulls relevant documents, runs the crew, and returns the response as JSON.
# app/main.py
from pydantic import BaseModel
from app.retrieval import retrieve_context
from app.crew import run_ib_crew
class QueryRequest(BaseModel):
question: str
@app.post("/rag/query")
def rag_query(payload: QueryRequest):
context = retrieve_context(payload.question)
answer = run_ib_crew(payload.question, context)
return {
"question": payload.question,
"context_snippet": context[:1000],
"answer": answer,
}
- •Run the API and wire it into your internal workflow.
Use Uvicorn locally first. Once stable, put it behind your standard API gateway or internal auth layer.
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
Testing the Integration
Hit the endpoint with a finance-specific query that should map to your indexed corpus.
import requests
payload = {
"question": "Summarize the key risks highlighted in the latest earnings transcript for Company X."
}
response = requests.post("http://localhost:8000/rag/query", json=payload)
print(response.status_code)
print(response.json()["answer"])
Expected output:
200
The latest earnings transcript highlights margin pressure from higher input costs,
softening demand in EMEA, and elevated capex tied to systems modernization.
These points should be reviewed against guidance before sharing externally.
If you get a generic answer with no grounding in your documents, check these first:
- •Your vector store contains the right source files.
- •Retrieval is returning relevant chunks.
- •The CrewAI task prompt explicitly restricts answers to retrieved context.
- •Your LLM temperature is low enough for deterministic finance responses.
Real-World Use Cases
- •
Deal desk Q&A
- •Analysts ask questions like “What are the main comps risks?” or “Summarize covenant constraints,” and get source-grounded answers from pitch books and CIMs.
- •
Earnings intelligence
- •Parse transcripts, retrieve relevant sections, and have CrewAI generate banker-style summaries for coverage teams.
- •
M&A diligence assistant
- •Combine document retrieval with multi-agent review to produce red flag lists from data rooms before management meetings.
The main design rule is simple: let FastAPI handle transport, let retrieval handle evidence selection, and let CrewAI handle reasoning and review. That separation keeps the system maintainable when you move from a prototype to something bankers actually use.
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