---
title: 'How to Build an Agentic AI Trading System with Heartbeat Monitoring'
source: 'https://youtube.com/watch?v=LNjrRJRCY_g'
video_id: 'LNjrRJRCY_g'
date: 2026-07-14
duration_sec: 0
---

# How to Build an Agentic AI Trading System with Heartbeat Monitoring

> Source: [How to Build an Agentic AI Trading System with Heartbeat Monitoring](https://youtube.com/watch?v=LNjrRJRCY_g)

## Summary

The video demonstrates setting up an agentic AI trading system using Cursor and sub-agents to monitor live trades via a heartbeat mechanism. The system uses a fast sub-agent (GPT-4.1 mini) to fetch position data and feed it to a main agent (GPT-4.5) every 30 seconds for decision-making. The video also showcases Better Stack as a caching tool to reduce API costs.

### Key Points

- **Problem with live agentic trading** [00:00] — Live trading requires a heartbeat to continuously monitor positions, which can be awkward to set up.
- **Sub-agent architecture** [00:30] — Use a sub-agent running on a fast, cheap model (GPT-4.1 mini) to fetch structured data from websockets, saving tokens for the main agent.
- **Sponsor: Better Stack** [01:30] — Better Stack provides caching between your app and OpenAI, reducing costs by avoiding repeated API calls for identical or similar queries.
- **Demo of Better Stack caching** [03:00] — Without cache: 1300 tokens used. With cache: 214 tokens used, showing significant savings via semantic and exact hits.
- **Setting up the heartbeat system** [04:30] — On HyperLiquid, a $50 margin 10x short on S&P 500 is opened. The sub-agent (GPT-4.1 mini) reads live heartbeat data and feeds it to the main agent every 30 seconds.
- **Goal-based trading** [06:00] — A goal of $1 profit in 30 minutes is set. The system adjusts risk based on the goal; a $10 goal would require higher risk.
- **Live trade execution** [07:30] — The main agent calculates strategy, fetches price, and places the trade. P&L is monitored, and the agent decides to hold or adjust.
- **Hedging example** [09:00] — A hypothetical hedge signal triggers a long Nvidia position (5x) as a partial equity hedge with a 25% hedge ratio, demonstrating adaptive decision-making.
- **Doubling down on strong signal** [11:00] — The agent exits the Nvidia hedge and doubles the S&P 500 short margin from $50 to $98, capitalizing on a perceived strong signal.
- **Future improvements** [12:30] — Plans to integrate /goal command for long-running tasks and improve the system further.

### Conclusion

The agentic AI trading system with heartbeat monitoring and sub-agents shows promise for autonomous trading. Future videos will explore the /goal command and further optimizations.

## Transcript

So over the weekend I've been playing more around with setting up my Aentic AI trading. I've been using codeex and I kind of ran into this problem because when we are kind of on something that is live let's say it's a trade could be on poly market your stock platform whatever if we want to do some aentic trading we need some kind of heartbeat right to monitor how we are doing all the time and this could be a bit of awkward so I kind of looked into what is the best way to set this up uh don't sure if I found it yet but I want to share today what I have set up. So uh on codeex for example this could be on cloud open code whatever we have this sub agents right so you can see here we have this sub agent and this could run on smaller models if we have this hard data pipeline so I ran my own 5.4 mini open ii GP 5.4 4 mini. This is a very fast model and it can do like really good on these structured outputs. Let's say we get some data from a web socket or something related to our position. So let's say our position is like a 10x short leverage on SP500. we can kind of mine that data and the sub agent can fetch that because we really want to save tokens when we feed that into kind of let's say our main model here right let's say it's codeex 5.5 so we want the sub agent to kind of monitor this and feed this into our codeex agent our main agent and it uses this heartbeat to check this data and make like a let's say like a new decision on our position let's say every 30 seconds or something. So, this is what I've been testing out and I think I found something that works at least. Maybe there's more work into it, but I wanted to share the setup and I'll show you how it works because I think this could be really interesting going forward playing more around with this creating like this very autonomous setup that could be hopefully profitable. So before we do that, speaking about context and stuff like that, uh this is a good time to talk about today's sponsor, Better DB. So first off, what is exactly better DB? So you can think of better DB as a monitoring observability and caching platform. And for AI apps, the big thing is that better DB gives you kind of two caches that kind of sits between your app and OpenAI, for example. So you stop paying for the same answer over and over again. So I created this demo I want to show you that kind of shows exactly of how you can actually use better DB. So this is the demo I built, right? So I set up my OpenAI key. Uh I have my model GPD 5.5. Uh I'm going to set the reasoning to low for this demo. But now you can kind of see uh we can run this now a live test. I'm going to send these five requests to OpenAI. Five different questions here. So we're going to run this uh first without the cache from better DB and then we're going to run it with better DB. So when we run the comparison here now you can see we send the first request to open AAI what is Valky this took 4 seconds before we got the response 197 tokens 7 seconds 4 seconds another or 350 tokens right and now you can see down here we spent 1300 tokens and you can see now we ran this with better DB the first one had to do one call to openi to kind of get the cash but after that we have some semantic hits we have an exact hit and you can see we only spent or we created 214 tokens and we save this amount per call, right? So, this is why I have been testing out better DB in my app. So, if you want to do the same, you can follow my link in the description and it's super easy to get started. Big thanks to Better D for sponsoring this part of the video. And let's get back to the project. So, let me show you now uh how I set up this. So, uh I think we're going to do it on Hyperlid. You can see I have about 900 and about $1,000 in the account. So let's say we open up our terminal here and we go into I just want to do codeex this time. Yeah, yolo. Okay. And you can see we are on GP 5.5 high. So what if we do sub agents and you can see we have one agent here. Sub agent. Great. So what we want to do now is uh I'm just going to read a markdown file I created to kind of set up this. I just called it heartbeat tradem and this has everything instructions how to actually run the main model as kind of the main agent and we have the sub agent that is uh kind of running on GPT 5.4 before and I I guess I could just say I can just say explain the sub agent what model is it running and the task and you can see uh maybe we can blow this up a bit maybe something like that and you can see the sub aent is a read only trade data reporter named trade data reporter it's intended to run as a on a fast model specifically specifically GPD 5.4 for a minute and it read files such as live heartbeat uh everything we get from the websocket. I'm just going to skip that and this it feeds into our main agent. The main agent uses a sub agent's JSON address as input. So we do like a refresh uh 30 seconds. So the main agent sleeps for 30 seconds gets the input from the sub agents make a decision on our live trade. Okay. So this is basically how we are set up. So why don't I just show you like a live example now of how this works. I think this is much better than me just talking about it. So let's say we wanted to do something like let's say we wanted to do something like this. So let's set a $50 margin SP500 short 10x leverage. So let's just do that. Let's also add the goal is to make $1 profit in 30 minutes. So we want the system to have something to aim for, right? Because if you're going to update the goal, it can make decisions based on the goal. So let's say we wanted to only make like here $1 profit in 30 minutes. This means that the decision it makes based on the heartbeat could be different if we wanted to make $10. Then it has to adjust risk, right? Because risk is based on if you're going to make $10 on a $50 margin in like 30 minutes, you have to take more risk. If we're going to do $1, we can do lower risk, but uh maybe yeah, do some other stuff. There's also something called slashgoal. Oops. Slash goal on um on codec. So, we can do like a set goal. This is also something I'm going to look more into. Okay. So, you can see here now Codex asked me like a question. What is the minimum accept or loss in dollars for this uh trade? So, I'm just going to say 100. And you can see we have our target profit. We have our trade setup here. Time stop 30 minutes. We're going to do one live leg. And we can add and we can add hedges. We can adjust positions. Lowering, tightening, risk, time, stop, exit. We can take money off the table and everything. So now I think we're just ready to set this position up and we're going to start monitoring. And you can see here Codex does some calculations. A $1 profit needs roughly a 15 point down uh move down. A literal $100 stop would sit likely be on liquidation. So I'm treating the 100 as the parent loss. So this is what we want to see. We want to do we want to see the agent calculate some kind of strategy before every trade. You can see it fetch the price is around this and it talks about this uh set uh trade we wanted and from this it can kind of calculate some initial parameters uh to actually reach our goal. So this is exactly what I wanted to see and I'm super happy that the system is even picking this up after I have kind of created it and run it in like a new different trade that we have done before. Okay, so I think we are ready to kind of place the order now and we should see a position here come up any time. Yeah, perfect timing. You can see we went in with $50 margin on 10x short on SP500. Perfect. So we should get like a confirmation here now from uh codeex that we are live and we should fire up our uh sub agent our heartbeat to monitor this. And you can see I'm launching the trade data reporter. So I mention it now and we'll use this to compact the facts for the 30 secondond parent uh heartbeat loop. Yes. And you can see we spawn this. We can see we are on GP 5.4 mini low. Okay. We could have been on maybe that's fine. And you can see this is running and we are waiting for this. This is now fetching our data from our websocket that is of course related to our position that is uh 10% done and finished waiting and you can see we got all this data and now our main agent that is going to not our sub agent of course is going to read all this you can see P&L is about plus 10 cents yeah that matches perfect and now uh it's going to do decision for this heartbeat is hold the path is still acceptable control and the configured TP is a clean exit path. Yeah, perfect. So, this is exactly what I wanted to see and this is looking quite good. Uh, but for the video sake, uh, we're not going to just keep watching this. That's a bit boring. Uh, but I want to show you an example of let's say we wanted to do a hedge now or let's say the signal was hedge. So, let's see what happens if we if we did that. So let's just do uh let's do uh how would you solve a hedge signal on the current trade with our goal in mind. So let's just see how codeex will handle this. This is of course not realistic but uh I wanted just to show you on the video uh how uh our model can adapt on the fly to like a hedge signal uh on the current trade here and what it would do if uh that was the case. So what is kind of interesting here is that uh you can see uh if the hedge signal was real I would not short long uh the S&P 500 I would use a separate corrected long probably uh Nvidia as a partial equity hedge so we're going to go the opposite end of short we're going to do long Nvidia and it's kind of using this numbers to calculate this I think this is quite important and a reasonable hedge shape would be around 25% hedge ratio so we're going to do like an Nvidia. So, it does some calculations here and it sets this up, right? And it's just going to fire this off and we're going to see we're going to take a long Nvidia position as a hedge to our yeah, I guess $1 goal here on the short position. And as I said that, you can see we went in here 5x Nvidia long uh as a hedge to our this position. So this is what I mean and this is exactly what I wanted to see because this kind of opens up a whole new kind of thing we can do going forward with this and I think it's going to be super interesting uh to actually follow this and see what kind of systems we can set up and try to automate this. This is kind of my end goal, right? So, we just want to say exit the Nvidia position hedge but double the margin on SP 500 as the signal is really strong. Now, let's capitalize. So, what we want agent to do now, if the signal was this uh let's say it saw that the SP signal was really strong, had good momentum, we could uh double down and kind of double our position just for a moment or something like that. So now I want to see we exit this Nvidia trade uh Nvidia hedge and we want to double our value here uh on the margin. Let's see if we can do that. And you can see for the SP500 the strong signal ad will be another 50 same side short at 10 add to position equals true. Okay, but remember we will get a different price now. But still you can see hopefully now we're going to exit this one and we're going to double our position value here. And you can see we did double this. Now we are on 98 margin. And now we're just going to exit this. And yeah, we are where we wanted to be. And there you can see we left that. And now we are kind of left with this. So I think that's uh everything I wanted to show off. And yeah, as you can see, we are up and running again. So that's great. So, uh, one thing I wanted to mention that next video or probably in a video in the future, we're going to look more into this slash goal because I think this is something we can implement with great success in this type of system. And this uh, use / goal when a task needs codeex to keep working across turns towards a verifiable stopping condition. So, I think this is very good for long running coding work and this is also good for the setup we have, but that's going to be a different video. So now I'm just going to close out the position. But basically uh this is what we had a look at today and I'm going to I'm going to look more into it and I'm going to come back with an update because this was kind of my initial take on this and I'm sure there are a lot of improvements. So if you want to see that uh please uh give this video a like, subscribe and I will keep you updated on all agentic AI trading what we learn about it and hopefully we can start making some interesting decisions. Uh don't forget to check out today's sponsor better DB if you want to do something with your tokens and your context control. Uh we have a growing community over on Discord. So feel free to drop by and if you want to talk anything about AI automation, AI genetic trading or something like that, you find the link in the description. We are yeah a lot of people here now. So it's going to be very interesting to see what people do. So thank you for tuning in. Have a great day and I'll see you again
