An AI-powered credit analysis agent built with LangChain Deep Agents that generates comprehensive credit investment memos for leveraged loans, high yield bonds, direct lending deals, and CLO tranches.
- Setup
- 1. Agent Architecture and Tracing in LnagSmith
- 2. Evaluations
- 3. Testing in Studio
- Project Structure
cd credit-investment-memo-agent
uv sync
cp .env.example .env # then add your API keysYou'll need:
- OpenAI API key β powers the orchestrator and subagents (GPT-4.1)
- Tavily API key or Perplexity API key β web search (free tier at tavily.com)
- LangSmith API key β tracing + sandbox + evals (smith.langchain.com)
ββββββββββββββββββββββββββββββββββββββββ
β Credit Memo Orchestrator β
β (Deep Agent β GPT-4.1) β
β β
β Tools: generate_memo_docx β
β read/save_market_intel β
β read_analyst_prefs β
β write_todos, task β
ββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββ
β β β
ββββββββββββββ β ββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββββββββ βββββββββββββββββββββββ βββββββββββββββββββββββ
β Research Subagent β β Data Subagent β β Calculations Subagentβ
β β β β β β
β web_search (Tavily) β β query_deals_db β β run_financial_ β
β rag_search (Chroma) β β get_db_schema β β calculation β
ββββββββββββ¬βββββββββββ ββββββββββββ¬βββββββββββ ββββββββββββ¬βββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β Tavily API β β SQLite β β LangSmith β
β + Chroma RAG β β deals.db β β Sandbox β
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Middleware β Memory (namespaced Store) β
β βββ TodoListMiddleware β ("analyst", id) β preferences β
β βββ FilesystemMiddleware β ("market", sector) β intel β
β βββ SubAgentMiddleware βββββββββββββββββββββββββββββββββββ£
β βββ SkillsMiddleware β
β βββ compliance_guardrail (custom @wrap_tool_call) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
All agent runs are traced in LangSmith. Set these in your .env to see each step of the agent execution as you invoke:
LANGSMITH_TRACING=true
LANGSMITH_API_KEY=your-key
LANGSMITH_PROJECT=credit-memo-agent
| Component | Purpose | Tools |
|---|---|---|
Orchestrator (create_deep_agent) |
Plans workflow, coordinates subagents, assembles final memo | generate_memo_docx, write_todos, task, memory tools |
| Research Subagent | Gathers public + internal qualitative research | web_search, rag_search |
| Data Subagent | Queries internal deal database for comps and exposure | query_deals_db, get_db_schema |
| Calculations Subagent | Runs financial math in a secure sandbox | run_financial_calculation |
- Multi-source research: Combines internet data (Tavily or Perplexity) with internal docs (RAG) and deal data (SQLite)
- Precise calculations: Financial metrics computed via LangSmith code sandbox β no LLM arithmetic
- Branded output: Generates formatted
.docxmemos using a structured template (Deep Agents Skill) - Citation tracking: Every fact tagged with its source, compiled into a Sources appendix
- Compliance guardrails: Custom
@wrap_tool_callmiddleware filters MNPI, enforces disclaimers, logs data access - Persistent memory: Analyst preferences and market intelligence persist across sessions via namespaced Store
The agent follows a 10-step workflow:
- Read analyst preferences and sector intelligence from memory
- Plan the memo workflow using
write_todos - Delegate research to the Research subagent (web search + internal docs)
- Delegate data queries to the Data subagent (comparable deals, portfolio exposure)
- Delegate calculations to the Calculations subagent (credit metrics, stress tests)
- Load the
credit-memo-templateSkill for formatting guidance - Synthesize all findings into the memo template sections
- Generate a formatted
.docxmemo and save toagent/output/
Steps 3 and 4 run in parallel. Step 5 depends on both.
| Namespace | Purpose | Tool |
|---|---|---|
("analyst", <id>) |
Analyst preferences, style, focus areas | read_analyst_prefs |
("market", <sector>) |
Sector trends, typical metrics, comps | read_market_intel / save_market_intel |
The compliance_guardrail middleware uses the @wrap_tool_call decorator to intercept every tool call:
- MNPI Filter β Scans tool outputs for material non-public information keywords and blocks them
- Disclaimer Check β Validates memos include required compliance language before writing
- Audit Logging β Logs every external data access to
agent/output/audit_log.json
The demo includes realistic dummy data seeded automatically on first run:
- 20 historical deals across Healthcare, Technology, Industrials, Consumer, and Energy sectors
- 10 portfolio holdings with current pricing and risk ratings
- 3 internal research memos (Acme Corp, GlobalTech Industries, Summit Healthcare Partners)
python agent/main.py "Generate a credit memo for Acme Corp"
python agent/main.py 'Analyze Summit Healthcare Partners for a new 200M dollar term loan'
python agent/main.py # interactive modeThe project includes a full evaluation suite with five evaluator types, a test dataset, and a runner script that supports model comparison. Results are visualized in LangSmith.
Evaluator Scores per Experiment:

| Evaluator | Type | What it checks |
|---|---|---|
structural_quality |
Code check | All 8 required memo sections present in the output |
trajectory_quality |
LLM judge | Tool call sequence is logical, arguments well-formed, execution efficient |
goal_achievement |
LLM judge | Memo achieves 10 specific goals (recommendation, metrics, risk analysis, citations) |
regex_patterns |
Code check | Formatting conventions β leverage ratios (4.7x), percentages (20.0%), dollar amounts ($300M), ratings (BB-), disclaimer |
pe_credit_diligence |
LLM judge | PE diligence criteria β sponsor involvement, stress scenarios, management, covenants, comps, sector risks |
Three examples covering different borrowers and deal types, uploaded to LangSmith:
| Borrower | Sector | Deal Type |
|---|---|---|
| Acme Corp | Industrials | Term Loan |
| Summit Healthcare Partners | Healthcare | Direct Lending |
| GlobalTech Industries | Technology | Leveraged Loan |
Each example includes reference outputs: required sections, 10 goals, expected tool sequence, regex patterns, and 7 PE diligence criteria.
# Upload the test dataset to LangSmith
python eval/dataset.py
# Run all evaluators β compares gpt-4.1-mini vs gpt-4.1 side-by-side
python eval/run_eval.py
# Custom experiment prefix
python eval/run_eval.py --experiment-prefix v2-with-perplexity
# Single model only
python eval/run_eval.py --single-model gpt-4.1
# Compare custom set of models
python eval/run_eval.py --models gpt-4.1-mini gpt-4.1 gpt-4.1-nanoEach model produces a separate experiment on the same dataset in LangSmith for side-by-side comparison. All evaluators return {"score": float, "comment": str} which renders natively in LangSmith's evaluation UI.
Open the project in LangGraph Studio to
visualize the agent graph, inspect state and memory at each node, and experiment interactively.
uv run langgraph devThis reads langgraph.json in the project root, which points to agent/graph.py:graph. The server automatically seeds the database, initializes the RAG vector store, and loads memory β no manual setup needed.
Generate a credit memo for Acme Corp evaluating a new $300M term loan opportunity.
Use reasonable assumptions for missing data. Do not ask questions, just produce the memo.
Analyze Summit Healthcare Partners for a potential $200M direct lending facility.
They operate outpatient surgery centers in the Southeast. Produce the full memo.
Generate a credit memo for GlobalTech Industries evaluating their BB- rated leveraged loan.
Do not ask questions, produce the memo.
```<img width="1194" height="1236" alt="Screenshot 2026-04-24 at 5 04 40β―PM" src="https://github.com/user-attachments/assets/2ec73ee8-4a8f-4b0c-9e10-6ce3e85c8b78" />
---
## Project Structure
βββ agent/ β βββ main.py # CLI entry point β βββ deep_agent.py # create_orchestrator() β Deep Agent config β βββ graph.py # LangGraph Studio entry point β βββ middleware.py # Compliance guardrail (@wrap_tool_call) β βββ subagents/ β β βββ research.py # Research subagent (Tavily/Perplexity + RAG) β β βββ data.py # Data subagent (SQLite) β β βββ calculations.py # Calculations subagent (sandbox) β βββ tools/ β β βββ web_search.py # Internet search β β βββ rag_retriever.py # Semantic search over internal docs β β βββ sqlite_query.py # SQL queries against deal database β β βββ sandbox_calc.py # LangSmith sandbox for financial math β β βββ memo_writer.py # python-docx document generation β β βββ memory_tools.py # Store-backed analyst/market memory β βββ skills/ β β βββ credit-memo-template/ β β βββ SKILL.md # Branded memo template + section guidance β βββ data/ β β βββ seed_db.py # Database seeder script β β βββ deals.db # SQLite database (generated) β β βββ internal_docs/ # Internal research memos β βββ output/ # Generated memo documents βββ eval/ β βββ dataset.py # Test dataset (3 examples) β βββ evaluators.py # Five evaluator types β βββ run_eval.py # Evaluation runner with model comparison βββ langgraph.json # LangGraph Studio config βββ pyproject.toml
