Return to Home
Deep ResearchAI & Machine LearningMay 21, 2026

Claude Code Financial Cheatsheet

Quantitative Finance & Architecture Detailed Reference — CLI flags, slash commands, MCP, sub-agents, security guardrails, and more.

Claude Code Financial Cheatsheet Infographic
Click to view full screen

CLI Execution Flags

Mastering the headless execution and context configuration flags.

Headless ETL Pipelines

claude -p "query" --json-schema '{"type":"object", ...}'

Forces Claude to exit immediately after outputting strictly validated JSON. Ideal for scheduled cron jobs extracting structured sentiment analysis from raw market news.

Compute Allocation

claude --effort max --model claude-3-7-sonnet

Triggers extended thinking tokens. Mandatory when asking Claude to debug complex math in stochastic calculus or Black-Scholes pricing models.

State Branching

claude -r <id> --fork-session

Resumes an existing trading algorithm discussion but forks the memory. Allows you to test two different neural network architectures without cross-contaminating the original session.

Cross-Repo Integration

claude --mcp-config ./mcp.json --add-dir ../lib

Mount external libraries (like your proprietary C++ quant modules) and load specific Model Context Protocol configurations in one command.

Runaway Protection

claude -p "Refactor" --max-turns 5 --max-budget-usd 2.00

Caps the autonomous agent loop in print mode. Prevents the model from burning budget if it gets stuck debugging a persistent Python compilation error.

Slash Commands (REPL)

In-session state, budget, and context management controls.

Context Compression & Wipes

/compact: Summarizes conversation history into a single dense prompt, clearing the raw backlog. Crucial when Claude accidentally ingests a massive API payload and approaches the 200k token limit.

/clear: Nukes the entire thread history but keeps the CLI open. Use when hard-pivoting from back-end C++ risk modeling to writing React front-end components to prevent context pollution.

Thread Multi-Tasking

/history: Displays a list of recent REPL sessions with their unique IDs.

/resume <id>: Hot-swaps your current context into a previous thread. Maintain one persistent thread for "AlphaVantage Pipeline Debugging" and another for "Options Greeks Math", switching seamlessly without losing momentum.

Budgeting & Diagnostics

/cost: Prints exact token usage, session duration, and API spend in USD. Mandatory before and after asking Claude to index large directories of Parquet files.

/init: Bootstraps a fresh CLAUDE.md architecture file in the root directory.

/bug: Packages terminal errors, current state, and prompt history to report systemic tool failures directly to Anthropic.

Resolving "Thinking Too Long"

Infinite tool-use loops are caused by Context Rot (token bloat), silent script failures, or overly restrictive negative prompting.

1Safe Interruption vs. Force Kill

Never force-kill (Ctrl+C) a runaway session — it severs the connection mid-tool-use and corrupts session state. Instead, press Alt+T (or Opt+T). This gracefully bypasses the extended thinking phase, forcing the model to output its current thought process and halting the loop safely.

2Diagnosing Tool Failure Loops

If Claude gets stuck, it is often trying to execute a bash script or Python file that is failing silently (e.g., swallowing stderr). Claude will blindly retry the tool indefinitely. Interrupt the model, manually read the script it wrote, fix the syntax error or missing dependency, and type /resume.

3Context Purging & Token Limits

When Claude starts randomly re-reading unchanged files like models.py over and over, its attention mechanism has degraded due to token bloat. Immediately run /compact to squash the history, or use /clear to keep your repo context but wipe the conversation thread.

4The @ Inclusion Trap

Avoid using global @ file references in your CLAUDE.md system prompt (e.g., @utils.py). This forces Claude to load the entire file into its context window on every single turn, rapidly exhausting the context limit and causing severe latency. Let Claude discover files via tools.

Handling Massive Data

Strategies to prevent token exhaustion when analyzing gigabytes of historical market data.

1. The Context Limit vs. RAM Reality

Claude's context window is ~200,000 tokens. A mere 5MB CSV of tick data will instantly crash the agent with a ContextLengthExceeded error. Never use claude "read data.csv" or dump raw API JSON arrays into the chat.

2. Bash Pipeline Sampling

Strictly constrain output using standard Unix tools before passing to Claude:

# DO NOT do this:
cat market_data.log

# INSTEAD prompt Claude to execute:
tail -n 1000 market_data.log | grep "ORDER_REJECTED" | head -n 20

3. Force DuckDB & Polars Usage

Add explicit instructions to your CLAUDE.md: "When analyzing datasets > 100MB, write a standalone Python script using DuckDB or Polars. Do not use Pandas. Execute the script to compute aggregate statistics and print only the resulting summary table."

This forces Claude to utilize memory-efficient SQL on Parquet files, reading only the results rather than the raw rows.

4. Implement Python Generators

When asking Claude to build backtesters for high-frequency strategies, instruct it to use lazy evaluation. Mandate the use of Python generators (yield) and memory-mapped files (np.memmap) to stream prices into the pricing model chunk-by-chunk, rather than loading millions of ticks into a list in RAM.

MCP Architecture

Model Context Protocol securely exposes internal APIs and databases directly to Claude's reasoning engine.

1. Global MCP Configuration (mcp.json)

Bind local or remote data servers to the CLI via configuration files rather than hardcoded scripts.

{
  "mcpServers": {
    "quant-db": {
      "command": "uvx",
      "args": ["mcp-server-sqlite", "--db", "historical_ticks.db"]
    }
  }
}

2. Market Data Abstraction

Instead of asking Claude to write brittle Python requests wrappers for Alpha Vantage or Polygon, connect an MCP server. Claude can dynamically call predefined tools like get_ticker_history(sym="AAPL"), ingesting the JSON output cleanly without writing networking code.

3. Secure Data Warehouse Queries

Provide Claude secure, read-only SQL access to Snowflake, Dolt, or PostgreSQL via MCP. This allows Claude to query proprietary Monte Carlo simulation results or portfolio state while keeping the actual database credentials entirely outside the LLM context window.

Sub-Agents & Plugins

Delegate tasks to isolated sandbox environments to parallelize workloads and protect context limits.

1. Defining Sub-Agents (.claude/agents/)

Define bespoke workers. Container isolation is crucial when running untrusted Python generated by the LLM.

# .claude/agents/backtest-runner.yaml
name: backtest-runner
model: claude-3-7-sonnet
isolation: container
tools: ["bash", "file_read", "file_write"]
system: "Run the backtrader suite. If Sharpe ratio < 1.5, adjust MACD params and rerun."

2. The 'Explore Agent' Pattern

Use Claude 3.5 Haiku as a high-speed, low-cost indexing agent. When onboarding to a massive legacy C++ HFT codebase, dispatch a Haiku sub-agent to traverse millions of lines of code to locate the specific slippage calculation functions, returning only the exact file paths to the main Opus/Sonnet reasoning agent.

3. Jupyter Integration Plugin

Utilize plugins like claude-jupyter. This allows Claude to spin up a headless Jupyter kernel, execute Python DataFrames for quantitative analysis in memory, and analyze generated Matplotlib/Seaborn charts without cluttering your local filesystem with .py scripts.

Financial Project Setup

Architecting the repo for deterministic LLM interaction.

CLAUDE.md (System Architecture)

The core global prompt. Organize into strict sections to govern agent behavior.

# Quant Role
You are a senior algorithmic trader. Optimize for speed.

# Stack
- Python 3.11, Polars (NOT Pandas), Numpy, Pytest.

# Constraints
- Never use float for currency; use decimal.Decimal.
- Do not mock market data; use /tests/fixtures/

.claudeignore Configuration

Critically important to prevent autonomous search from reading massive binaries, crashing the context window, or leaking secrets.

  • *.csv / *.parquet
  • *.sqlite / *.db
  • *.h5 / *.feather
  • /historical_tick_data/
  • /notebooks/.ipynb_checkpoints/
  • .env.production

Hooks & Declarative Skills

Injecting guardrails and reusable mathematical workflows.

Lifecycle Hooks (.claude/hooks/)

Executable scripts that intercept Claude's tool usage.

  • pre_tool_use.sh: Compliance Gates. Intercept the Bash tool. If the proposed command contains rm -rf or attempts to read AWS_ACCESS_KEY, exit with status 1 to block execution.
  • post_tool_use.py: Auditing & Formatting. Automatically pipe generated Python files through black and ruff.
  • stop.sh: Triggers when Claude finishes. Force-run pytest tests/pricing/ before allowing the session to conclude.

Declarative Skills (SKILL.md)

Stored globally in ~/.claude/skills/. Used to package complex analytical workflows.

Example (backtest-analyzer.md):
Contains a predefined prompt template outlining the formula for Sharpe and Sortino ratios. When invoked via /backtest-analyzer result.json, Claude automatically ingests the JSON and outputs standardized risk metrics without needing manual prompt engineering.

Plan Mode & Workflows

Strategies for safely managing massive architectural refactors and backtest deployments.

Plan vs. Auto Mode Workflow

Launch with claude --permission-mode plan --model claude-3-7-sonnet for read-only analysis. Claude will search files and generate architectural diagrams detailing how it intends to implement a new multi-leg options strategy.

Because it lacks write permissions, it cannot accidentally overwrite legacy pricing logic. Once the proposed plan is approved by the human quant, run /act for the rote execution phase.

"Wave-Based" CI/CD Implementation

Essential for massive codebases where single sessions hit the 200k token context limit.

  1. The Architect (Core Agent) Reads quant requirements, creates the structural plan, and breaks the mathematical implementation down into isolated, sequential GitHub Issues.
  2. The Execution Wave (Sub-Agents) The Core agent spawns isolated sub-agents (Haiku models for speed) to write the code for each issue in parallel worktrees. Crucially, these agents are given a strict kill-switch at 75% context capacity to force a commit and prevent hallucination loops.
  3. The Review Loop The Core agent aggregates sub-agent outputs and runs Pytest suites. If the math fails, it wipes the specific Haiku context and spawns a fresh agent with the exact test failure logs.

Security & Compliance

Mandatory institutional guardrails. Treat the CLI as a privileged user.

The "Compiler" Analogy (Reproducibility)

Never let the LLM execute live trades autonomously via an MCP tool. LLM outputs are inherently non-deterministic. Treat Claude purely as a "compiler" and code generator.

All actual order routing must occur via highly-tested, deterministic C++/Python runtimes triggered by standard CI/CD deployment jobs, keeping the LLM entirely isolated from the exchange API keys.

PII, PCI & Config Scrubbing

Because Claude Code sends local file context to Anthropic's cloud servers, ensure .claudeignore aggressively blocks:

  • Local development databases (SQLite, .db files).
  • ~/.aws/credentials or .env files containing live account numbers.
  • Directories containing unanonymized client trade histories.

OS Sandboxing & Network Routing

Run the Claude CLI within strictly configured OS-level sandboxes:

  • Linux: Run via bwrap (Bubblewrap) or inside a minimal, unprivileged Docker container.
  • macOS: Utilize Apple sandbox-exec (Seatbelt).
  • Network: Use HTTP_PROXY=http://10.x.x.x claude to forcefully route traffic through corporate VPC gateways, enforcing strict "Deny All" outbound policies to unapproved domains.

Read the Full Research Paper

Access the complete reference document with extended CLI examples, MCP configuration templates, and detailed security implementation guides.

Read Full Research Paper

This article is for educational and informational purposes only. It does not constitute financial, investment, or legal advice. All code examples are illustrative and should be reviewed by qualified engineers before production deployment.