All About AI

Building an Agentic AI Trading Heartbeat That Works

The single biggest open problem in my agentic trading work has been the heartbeat — how do you keep a live position monitored every 30 seconds without burning tokens on the same websocket data over and over? I spent the weekend on it and landed on a split-agent setup that finally feels right: a tiny fast sub-agent doing the data work, a strong main agent doing only decisions. Demo on Hyperliquid below, including the agent computing a real-time hedge with a 25% ratio without prompting.

Watch the video:

Quick disclosure: BetterDB sponsored this video. They're a semantic + exact cache layer between your app and OpenAI that drops token cost on repeat-shape queries — useful when a heartbeat loop hits the model with similar prompts every 30 seconds. More on that below.

The problem with naive heartbeats

Most of my early agentic-trading setups had the main model do everything — fetch the live position, parse the websocket frames, evaluate against the strategy, decide. That works for one or two cycles, then it gets expensive fast. A 5-minute monitoring loop firing every 30 seconds is 10 model calls. Each call re-ingests the strategy doc, the position context, the recent ticker history, the goal. Most of those tokens are identical between calls. You're paying to re-tell the model what it already knows.

The fix isn't a smarter prompt. The fix is splitting the loop into two roles with very different cost profiles.

The split-agent shape

Codex CLI (and Claude Code, and Open Code) all support sub-agents — child processes the main model can spawn with their own model selection and scope. I run the heartbeat like this:

Token cost on the main agent goes way down because the data-shaped portion of the prompt is now a small JSON object instead of raw websocket history. Sub-agent runs are nearly free because mini handles structured summarization fine. The decision quality stays on the strong model because that's where it matters.

This is the same pattern I used in building the Hyperliquid agent for research, just inverted in scope — there the sub-agents did expensive parallel research; here they do cheap continuous polling. Same skill-pipeline shape from the 3-part AI agent system.

BetterDB — the second cost lever

The other thing the heartbeat does a lot is ask semantically similar questions. "Given current P&L $0.10 and 15 minutes remaining on a $50 margin SP500 short, should I hold?" gets asked again at 14:30 remaining, 14:00, 13:30… same shape, slightly different numbers. With BetterDB sitting between the agent and the OpenAI call:

I ran their demo with five different questions to OpenAI: first time cold, ~1300 tokens spent across all five. Second time through BetterDB, the first call seeds the cache, the rest hit semantic matches — total: ~214 tokens. That's roughly an 80% reduction on a workflow that legitimately should benefit. Not all queries cache cleanly (decisions involving actual numbers shouldn't be cached), but the framing prompts and the strategy-context lookups are perfect candidates.

Free tier is generous enough to test it on real work before paying. Link in the resources.

Live demo — SP500 short with a $1 goal

The setup, dictated to Codex:

$50 margin, SP500 short, 10x leverage. The goal is to make $1 profit in 30 minutes. Read heartbeat-trade.md for the main + sub-agent setup.

Codex asked one clarifying question — minimum acceptable loss in dollars — I said $100. It then computed: a $1 profit needs about a 15-point S&P move down, and a literal $100 stop would sit at near-liquidation given the 10x leverage, so it would treat the $100 as a parent-loss limit rather than a hard stop. That calculation happened before any trade fired. This is exactly the layer I wanted — math before action.

Position opened: 10x short SP500, $50 margin. Sub-agent spawned, GPT-5.4 mini on low. First heartbeat 30 seconds later returned a JSON summary, main agent's verdict: "hold, the path is still acceptable, configured TP is a clean exit path." Matches what I'd expect a careful trader to think. The thing is now running on its own.

The hedge test — emergent reasoning

To stress the loop I asked the main agent: "how would you solve a hedge signal on the current trade with our goal in mind?"

It came back with a reasoned hedge structure unprompted:

This is the kind of reasoning I'd want from a junior trader, not a default model behavior. The fact that it composed it on the fly from "you have a position, here's a goal, here's a signal" rather than from a pre-coded hedge playbook is the property I care about most.

The double-down test

Next signal: "exit the NVDA hedge but double the margin on SP500 — signal is strong, capitalize." The agent:

  1. Closed the NVDA long
  2. Added another $50 margin on the same-side SP500 short, recognizing the price would be different at this entry
  3. Total position margin now $98 at unified 10x
  4. Confirmed back what it had done in plain language

Then "exit everything" — out clean.

This is the loop I've been wanting. Not because it makes money — that's a separate question and one I'm running longer experiments on — but because the shape of the agentic trading workflow is correct. Main agent reasons in plain language at the speed of a human, sub-agent does the cheap continuous work, real positions get sized and adjusted with real math.

What's next — /goal and longer horizons

The Codex CLI has a /goal slash command I haven't fully explored — it sets a verifiable stopping condition that Codex keeps working toward across multiple turns. The fit for trading loops is obvious: /goal $1 profit in 30 minutes on the current position, then Codex won't return control until the goal is hit or the timer expires. Cleaner than the manual heartbeat I'm running now.

Beyond that, the obvious longer experiment is running this setup for days, not minutes, across multiple positions. The model-comparison work in the Hyperliquid head-to-head showed that active-monitor behavior is what separates Codex from Opus on these tasks — this split-agent heartbeat formalizes that into a setup any model with sub-agent support can run.

Updates on the longer runs land first in the AI_automata Discord.

Resources