Stateless Memory Architecture Powers Persistent Agent Behavior

Claude Code routines run stateless each time—they wake up, read files, act, then write updates. This file-based memory (e.g., strategy.md, trade_log.md, research_log.md, portfolio.md) simulates learning without servers or databases. Every routine starts by ingesting key files into context, enforcing discipline across invocations.

"The trading strategy is a piece of the work, but the memory architecture that you're going to use is going to be huge. Every time a routine fires at 7 a.m., cloud code basically wakes up essentially stateless. It doesn't really know anything. So, how do you make a stateless agent act disciplined and remember rules and learn over time? You do that with files and with context."

Manage context budget tightly: aim under 200k tokens per run despite 1M windows, avoiding rot. Prioritize system instructions (10-20k), strategy (5k), logs/portfolio (variable), research (fresh). Use markdown for structured, parsable persistence—e.g., append-only logs with dates, no overwrites to prevent loss.

Common pitfall: Dumping everything into one mega-file causes token bloat and incoherence. Solution: Modular files (one per concern) read selectively per routine. For example, pre-market reads strategy + prior logs; execution reads portfolio + fresh research.

Fundamentals-Driven Strategy Beats S&P via Phased Training

Define a clear, rule-based strategy first—don't expect magic. Migrate from prior agents or brain-dump your manual process: news checks, buy/sell signals (e.g., earnings beats, sector momentum), position sizing (1-5% risk), stops (5-10% trailing).

Target long-term holds beating S&P, leveraging Opus 4.7's 64.4% Agentic Financial Analysis benchmark for digesting filings into theses, not day-trading candlesticks. Start paper trading: Alpaca provides $100k sim with live data.

"This benchmark rewards models that can digest filings and write coherent fundamentals driven theses... And that maps to long-term or swing or fundamentals driven strategies, not day trading."

Phased rollout like teaching a kid to bike: (1) Observe/simulate, (2) Paper trade with guardrails, (3) Live micro-positions, (4) Full autonomy. Hard rules gate trades: max 10% portfolio exposure, no shorts initially, verify signals twice.

Quality criteria: Logs must justify every decision with evidence (news links, ratios). Backtest via routine reviews. Iterate: Weekly Fridays analyze P&L, refine signals (e.g., avoid over-reliance on hype sectors).

Scaffold Setup: Migrate Knowledge into Modular Files

Create a VS Code project with Claude Code extension for file visibility (desktop app lacks sidebar). Core files from prior agent migration or scratch:

  • strategy.md: Signals, rules, position sizing.
  • trade_log.md: Timestamped entries (action, symbol, size, reason, P&L).
  • research_log.md: Summarized news, theses.
  • portfolio.md: Current holdings, cash, performance vs. S&P.
  • weekly_review.md: Trends, adjustments.
  • .claude/settings.json: Env vars (API keys hidden).
  • README.md: Overview.

Prompt Claude in plan mode: "Migrate this OpenClaw strategy—ingest files, propose layout, rotate keys." It reorganizes, adds routines stubs. Free 13-page PDF (Skool classroom) templates folder structure, cron ideas.

"I just dropped in a ton of context for you... ingest this information and... organize this project in the way that makes the most sense to you."

Pitfall: Hardcoding keys—use env vars. Brainstorm iteratively: Answer Claude's questions on ambiguities (e.g., risk tolerance) to refine.

Guardrails and Custom Skills Enforce Discipline

Guardrails as system-level rules in .claude/system.md: "Never trade without dual verification. Max 5% per position. Log everything. No crypto/day trades."

Build skills as reusable prompts in skills/ folder:

  • research_skill.md: Query Perplexity API for news/earnings on watchlist (e.g., NVDA, sector leaders).
  • decision_skill.md: Score opportunities (fundamentals + momentum).
  • trade_skill.md: Alpaca endpoints—POST /orders (market/limit), GET /positions, set stops via bracket orders.
  • notify_skill.md: ClickUp task creation for recaps.

API integration: Env vars for ALPACA_KEY, PERPLEXITY_KEY, CLICKUP_TOKEN. Claude crafts curl/fetch calls dynamically.

"Hard strategy rules gate every order before it fires."

Test skills standalone: Prompt "Simulate buy AAPL with current portfolio." Ensures outputs are structured JSON for parsing.

Five Routines Cover Full Trading Cycle

Schedule via Claude Desktop app calendar (paid sub required):

  1. 6AM Pre-Market: Read logs/portfolio, Perplexity research on watchlist/news, update research_log, flag opportunities. No trades.
  2. 8:30AM Open: Review pre-market, execute buys/sells if signals align, set stops, log trades, notify ClickUp.
  3. Noon Midday: Scan holdings, adjust stops on winners, research intraday shifts.
  4. 3PM Close: P&L calc, journal lessons, S&P compare, daily recap to ClickUp.
  5. Friday Weekly: Aggregate week, backtest signals, propose strategy tweaks.

Each routine: Read files → Research/decide → Act (if gated) → Write updates → Notify. Cron via app: Daily except weekly.

Adapt for your flow—e.g., swap Telegram for ClickUp. Pitfall: Over-scheduling erodes context; start with 2-3.

Deployment: Desktop App to VPS for Reliability

  1. GitHub repo project (gitignore keys).
  2. Claude Desktop: New routine → Link repo → Set env vars → Calendar schedule.
  3. Test: Manual run, verify logs/trades.
  4. Scale: Hostinger VPS ($10% off NATEHERK) for 24/7 uptime (Claude Code extension).

"Five cloud routines handle the full trading day: pre-market research, market-open execution, a midday scan, an end-of-day summary, and a Friday weekly review."

Monitor: ClickUp recaps flag issues. Rotate keys post-migration. Quality: 8% S&P beat in 30 days prior; aim consistent via iteration.

Key Takeaways

  • Start with paper trading on Alpaca—verify account for live later.
  • Build memory via modular markdown files: Read at start, append at end.
  • Define 5-10 hard rules (e.g., 5% stops) before any autonomy.
  • Use Perplexity for research, Alpaca for trades—hide keys in env vars.
  • Schedule 4-5 routines: Pre-market research, open execution, midday check, close recap, weekly review.
  • Migrate prior agent knowledge via file dumps and Claude brainstorming.
  • Budget <200k tokens/run: Prioritize strategy/logs over fluff.
  • Iterate weekly: Analyze P&L, refine signals from logs.
  • Test skills individually before routines.
  • Phased rollout: Simulate → Paper → Live small → Full throttle.