Risk and Compliance Guardrails
Position sizing limits, prohibited instrument checks, and audit logging.
Why Financial Agents Need Hard Guardrails
A financial agent that can recommend any trade without restrictions could expose users to catastrophic losses or regulatory violations. Unlike a human advisor, an agent will execute its recommendation logic consistently — which means a bug or misconfiguration affects every user.
Hard guardrails prevent the worst outcomes.
Position Size Limits
Limit how much of the portfolio the agent can recommend putting into any single asset. A common rule: no single position should exceed 20% of total portfolio value. This prevents catastrophic concentration risk.
MAX_POSITION_PCT = 0.20 # 20% of portfolio max per asset
def check_position_size(ticker: str, trade_usd: float,
portfolio_value: float) -> dict:
proposed_pct = trade_usd / portfolio_value
if proposed_pct > MAX_POSITION_PCT:
return {
'allowed': False,
'reason': (
f'Position size {proposed_pct:.1%} exceeds limit {MAX_POSITION_PCT:.1%}. '
f'Max trade for this portfolio: USD {portfolio_value * MAX_POSITION_PCT:.0f}'
)
}
return {'allowed': True, 'position_pct': round(proposed_pct, 4)}All lessons in this course
- Market Data API Integration
- Portfolio Analysis Agent Tools
- Risk and Compliance Guardrails
- Backtesting Agent Decisions