M
Mastra•4w ago
DuhKneeL

Dynamically Reload Prompt/Agents

Hi team, is there a way to dynamically reload the agent instructions (or the agent itself)? We're using Langfuse for prompt management and would love to automate the propagation of prompt changes from Langfuse to Mastra. Our current workaround is to simply trigger an infra-level restart of the pod (we don't have much volume yet so it's fine). I'm wondering if there was a way to do this without manual intervention? I'm picturing a simple custom route implementation:
apiRoutes: [
registerApiRoute('/prompt-chanve', {
method: 'POST',
handler: async (c) => {
const mastra = c.get('mastra');
const agent = mastra.getAgent('simpleAssistant');
agent.refreshInstructions(); // or reload the entire agent
},
}),
],
apiRoutes: [
registerApiRoute('/prompt-chanve', {
method: 'POST',
handler: async (c) => {
const mastra = c.get('mastra');
const agent = mastra.getAgent('simpleAssistant');
agent.refreshInstructions(); // or reload the entire agent
},
}),
],
Thank you!
7 Replies
Mastra Triager
Mastra Triager•4w ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10212 šŸ” If you're experiencing an error, please provide a minimal reproducible example to help us resolve it quickly. šŸ™ Thank you @DuhKneeL for helping us improve Mastra!
Abhi Aiyer
Abhi Aiyer•4w ago
Hi @DuhKneeL are you passing the instructions within a function? or passing it straight up? new Agent({ instructions: () => langfuse.prompt() }) or new Agent({ instructions: langfuse.prompt() })
DuhKneeL
DuhKneeLOP•4w ago
Hi @Abhi Aiyer, thanks for taking a look!. I'm passing the instructions straight up. Are you suggesting that with a dynamic instruction resolver, remote Langfuse updates would automatically propagate to the Mastra agent?
export const simpleAssistant = new Agent({
name: 'Assistant',
instructions: async () => {
const promptObj = await langfuseClient.prompt.get('assistant');
return promptObj.prompt;
},
});
export const simpleAssistant = new Agent({
name: 'Assistant',
instructions: async () => {
const promptObj = await langfuseClient.prompt.get('assistant');
return promptObj.prompt;
},
});
Vulcano
Vulcano•4w ago
@DuhKneeL if you give the instructions as a function, you can change them based on the runtimeContext, which is only available at the time of doing the request to the agent, which would of course pull your latest prompt from Langfuse (you could also have a beta tag/version for your Langfuse prompts and provide those in your frontend in the runtimeContext, so users on the production prompt dont get a broken product)
Abhi Aiyer
Abhi Aiyer•3w ago
@DuhKneeL yeah i'm suggesting putting it in the instructions as a function so it gets evaluated. And @Vulcano 's point is good too! It can give you some more control in the future based on runtimeContext
DuhKneeL
DuhKneeLOP•3w ago
Wow, this is amazing! Wasn't aware of the functional approach. Thanks so much everyone! šŸ™‡ā€ā™‚ļø
Abhi Aiyer
Abhi Aiyer•3w ago
Anytime dude!

Did you find this page helpful?