M
Mastra•2w ago
619napster

Does Runtime Context is persistent across Messages

Lets say I set a value in runtime Context and in the next message I get the value from runtime context will that work ?
No description
No description
14 Replies
Vulcano
Vulcano•2w ago
@619napster what do you mean exactly with 'next message'? It feels from your example like these could be workflow steps, in which case the runtimeContext is passed through and therefore variables are persisted. But if these are completely separately invoked calls, the runtimeContext is not the same. That is exactly the point of the runtimeContext: to give context of the runtime that the workflow/agent is currently running in
619napster
619napsterOP•2w ago
No description
619napster
619napsterOP•2w ago
I hope my image explains
Vulcano
Vulcano•2w ago
You are using an agent, which means every time you send a message you do a completely fresh invokation of the mastra agent So in your case, the runtimeContext is (and should be) reset every time
619napster
619napsterOP•2w ago
oh, so is there anything if i set it on the thread level it stays there
Vulcano
Vulcano•2w ago
If you want your agent to persist data accross messages, you will either need to look into a workflow that stores it as state (and have the agent use the workflow internally) or if you want to just use an agent: look into Mastra's Memory solutions
619napster
619napsterOP•2w ago
memory: new Memory({ storage: new PostgresStore({ connectionString: process.env.DATABASE_URL!, }), options: { threads: { generateTitle: true, }, workingMemory: { enabled: true, } }, }), so if I set this it will still reset the context on each message ?
Vulcano
Vulcano•2w ago
no, memory is not reset per message. Memory is on thread-level You can also use a working memory template:
const testMemory = new Memory({
options: {
workingMemory: {
...
template: 'Current advertiser: -'
}
}
})
const testMemory = new Memory({
options: {
workingMemory: {
...
template: 'Current advertiser: -'
}
}
})
which might be nice in your case, since you want the agent to remember a specific bit of information
619napster
619napsterOP•2w ago
Just need a little more help I am able to set the template and its updating
No description
No description
619napster
619napsterOP•2w ago
so how to use this value in tool call ?
Vulcano
Vulcano•2w ago
if you ask for currentAdvertiserId in your tool input schema the agent can pass it as a parameter because it has it in memory
619napster
619napsterOP•2w ago
I am Blown Away ! it works ! Coming from LangChain Python, it feels like LangChain is 1995 while Mastra lives in 2030
Mastra Triager
Mastra Triager•2w ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10548 šŸ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. šŸ™ Thank you for helping us improve Mastra!
Abhi Aiyer
Abhi Aiyer•2w ago
Thank you for the compliment @619napster ! And thank you so much @Vulcano for the assist!!

Did you find this page helpful?