Bubble & OpenAI - Send only recent messages
In this Bubble tutorial we demonstrate how you can lower your OpenAI token spend by just sending the most recent messages to the OpenAI API. This method will ensure your message still has some awareness of previous messages without sending the oldest messages.
Limit OpenAI messages: Learn the secret to optimizing your OpenAI conversations in Bubble!
Master Bubble workflows: Discover how to send only the most recent messages and avoid token limits with this workflow hack.
Boost your AI chat app: Unlock the power of context-aware conversations while keeping your OpenAI requests efficient.
Limiting Messages Sent to OpenAI API
Thank you to everyone who has subscribed, liked and commented on the video. It really helps our YouTube channel and us to continue to create fantastic content around Bubble and low-code development. One of the comments we received was asking a question about OpenAI and Bubble and that is how can I limit the number of messages that I send through to the OpenAI API? And that's because even if you increase the token limit to say the 16k models, you will eventually get to a point where the conversation with your user exceeds the number of tokens that you can send back over to OpenAI because remember the OpenAI requires you to send all of the history of all of the messages of that conversation in order for it to be aware of what's been said in previous messages.
Implementing a Message Limit
I'm going to show you how very quickly you could limit it to say the last 10 messages so that there is still some contextual historical awareness in the conversation, but you're not sending 60 messages. You're only sending 10. So I'm going to adapt this workflow here. This is one that I've demoed in plenty of other OpenAI videos. If you're not sure how to get to this point go and check those ones out. But I basically create a message that's the one that the user has created. I then send all of the messages that have been between the user and the AI. I then reset the inputs and I create a new message. That's the AI's response. This is the box I'm going to change. I'm going to set it to only send the last 10 messages.
Sorting Messages by Date
I'll go back to just search for messages. If you were to constrain them by add a constraint by conversation, you would have that in here. But I'm going to do something such as created date descending yes. What this will do is this will put the most recent message first. This is the wrong way around for OpenAI. OpenAI is expecting like any other chat application for the oldest to be up top and then for it to get newer as the conversation goes down to the bottom. So the last message should be the most recent.
Using Items Until for Message Limit
I'm flipping it around and that's because of a limitation that Bubble gives us here which it gives us items until and items from. The items from is only going to be useful in our calculation of the last 10 if we're keeping a count of how many messages there are. So if there have been 40 messages, we need to know there's been 40 messages to take 10 from that leaving us with 30 and then we could say items from 30. Now you could keep some sort of running count in your app. Maybe you could keep a count on the conversation. In that case, yeah, you could make it work. But you could also just use what I'm about to show you with items until. So I'm going to say items until 10.
Reversing the Order for OpenAI
And so what that will do is it's going to start with the most recent message and it will then show the previous 10. So message one is the most recent. Message two is the one before that all the way through to message 10 which will be the oldest message but of that most recent block of 10. It's still the wrong way around for OpenAI. So then what I can do is I can say sort. So now that I've got this operator of getting the most recent 10 messages, they're in the wrong order. So now I've put them back in the right order which is created date descending note. So now they're again in the right chronological order for OpenAI.
Formatting Messages as JSON
And then I can add in my format as text and this is how I get my JSON expression. And to do that I'm just going to pop into the OpenAI documentation and copy this part here. The delimiter that's the computer science term for what you put in between. So that's going to be a comma and a space and then roll. I simply need to put I'm not sure in this demo if I have this set up. Basically this message. This message roll. There we go. So that's again if it's system assistant or user and then the context. Remember I'm making this all JSON safe. We don't want to be getting those error 400s because the syntax issues with our JSON.
Finalizing the Message Format
So I'm going to get rid of all of that. And the context is this content is this message is text JSON safe. So remember we format it like this because JSON safe encloses the content in speech marks. And also it will escape it will make safe any characters in the content that the user has typed in that could cause the syntax error JSON safe. So set for us. So. Yeah they have it. That's how to send the most recent 10 messages. You reverse the order get the first 10 reverse the order back and then format as text.
Conclusion and Call to Action
And if you find this video useful and you're learning Bubble you need to check out our website planetnocode.com. We've got hundreds of Bubble tutorial videos at the moment we're releasing a new video basically every day of the week. Our library is growing and growing and growing and we've got so much that we love to teach you about building a Bubble and many of those videos are exclusive to our members. And you can find out more at planetnocode.com.
Ready to Transform Your App Idea into Reality?
Access 3 courses, 400+ tutorials, and a vibrant community to support every step of your app-building journey.
Start building with total confidence
No more delays. With 30+ hours of expert content, you’ll have the insights needed to build effectively.
Find every solution in one place
No more searching across platforms for tutorials. Our bundle has everything you need, with 400+ videos covering every feature and technique.
Dive deep into every detail
Get beyond the basics with comprehensive, in-depth courses & no code tutorials that empower you to create a feature-rich, professional app.
Valued at $80
Valued at $85
Valued at $30
Valued at $110
Valued at $45
14-Day Money-Back Guarantee
We’re confident this bundle will transform your app development journey. But if you’re not satisfied within 14 days, we’ll refund your full investment—no questions asked.
Can't find what you're looking for?
Search our 300+ Bubble tutorial videos. Start learning no code today!
Frequently Asked Questions
Find answers to common questions about our courses, tutorials & content.
Not at all. Our courses are designed for beginners and guide you step-by-step in using Bubble to build powerful web apps—no coding required.
Forever. You’ll get lifetime access, so you can learn at your own pace and revisit materials anytime.
Our supportive community is here to help. Ask questions, get feedback, and learn from fellow no-coders who’ve been where you are now.
Absolutely. If you’re not satisfied within 14 days, just reach out, and we’ll issue a full refund. We stand by the value of our bundle.
Yes, this is a special limited-time offer. The regular price is $350, so take advantage of the discount while it lasts!