AI Gateway
Financial Guardrails & Number Validation
Why Financial AI Needs an Orchestration Layer
In a general-purpose chatbot, a wrong answer is annoying. In a financial AI system, a wrong number can drive real decisions — portfolio rebalancing, earnings estimates, competitive positioning. The AI gateway is the safety layer that prevents bad outputs from reaching analysts.
This module builds the gateway as a LangGraph.js state graph — a directed graph where each safety check is a visible, debuggable node. The graph architecture means you can trace exactly why a particular number was flagged or a disclaimer was added.
The Financial Gateway Graph
classify_query → check_cache → [hit] → return_cached
→ [miss] → apply_guardrails
→ check_freshness
→ route_model
→ call_llm
→ validate_numbers
→ add_disclaimers
→ track_usageEach node is a pure function: it reads the current state, performs its check, and returns updated state. Edges between nodes can be conditional — the graph takes different paths based on query classification.
Financial-Specific Nodes
Query Classification
Financial queries fall into distinct categories that require different handling:
| Query Type | Example | Model Needed | Special Handling |
|---|---|---|---|
| Factual lookup | "NVDA Q3 revenue?" | Fast (Haiku) | Exact number verification |
| Comparison | "Compare NVDA vs AMD margins" | Capable (Sonnet) | Multi-company context assembly |
| Trend analysis | "Revenue growth last 4 quarters" | Capable (Sonnet) | Temporal ordering, calculation |
| Forecast/opinion | "Will margins improve next quarter?" | Strongest (Opus) | Heavy disclaimers required |
| Screening | "Which companies have P/E < 20?" | Fast (Haiku) | Structured data filters |
The classifier extracts: query_type, tickers_mentioned, metrics_requested, time_period, and complexity_level. This metadata drives every downstream decision.
Number Validation
The most critical financial node. After the LLM generates a response, this node:
$X.XM, XX.X%, X.XX, dollar amounts, percentages, ratiosIf any number is contradicted, the node can either flag it in the response or trigger a re-generation with explicit instructions to use only source data.
Freshness Checking
Financial data has strict freshness requirements:
| Data Type | Acceptable Age | Action When Stale |
|---|---|---|
| Market data | < 1 hour | Flag with "as of [timestamp]" |
| Earnings transcripts | < 90 days | Acceptable for current quarter |
| SEC filings (10-Q) | < 100 days | Flag if newer filing expected |
| SEC filings (10-K) | < 1 year | Flag with "annual data from [date]" |
| Analyst notes | < 30 days | Flag if older than most recent note |
The freshness node checks the data_date metadata of all retrieved chunks against these thresholds. If the data is stale for the query type, it adds a freshness warning: "Note: This analysis is based on Q2 2024 data. Q3 2024 10-Q has not been filed yet."
Disclaimer Management
Financial AI must never provide investment advice. The disclaimer node adds context-appropriate notices:
Caching with Financial TTLs
Not all financial data ages at the same rate. The cache node uses source-type-aware TTLs:
Cache keys include the query embedding, ticker filter, and period filter. Two queries that are semantically identical but for different tickers must be separate cache entries.
Cost Controls
Financial analysis queries can be expensive — complex comparisons may use 10,000+ tokens of context. The gateway enforces:
LangGraph Benefits
Building this as a LangGraph state graph instead of nested if/else provides:
This is chapter 4 of AI Finance Analyst.
Get the full hands-on course for $100 and build the complete system. Your projects become your portfolio.
View course details