https://www.boundaryml.com logo
🟡 Implementing Prefix Caching in BAML's Agent
# questions
v
How can Prefix Caching be implemented in BAML's agent? I am currently using a dynamic schema where tools are injected dynamically, but this causes the system prompt to be disrupted, leading to caching failure.
c
Caching is implemented on the model side and not by BAML. Unless you are using an on-premise/Your-Own-Custom-Model , there is nothing to be done from BAML side. There are some ways to address it though: - Keep your static content at the top of the prompt - Keep the dynamic content at the end of the prompt. In your case, the tools if it keeps changing should be at the bottom of the prompt. Sort the tools list before you inject it into the prompt, so it is not random all the time That way you will get cache hit even though the tools keep changing
v
Yes, I understand what you're saying. I know this is an implementation on the model side. I'm just thinking about how to use BAML to achieve best practices for caching. Thank you for your reply.
For the prompt section, there is currently a tricky issue: BAML can only treat the ctx output format as a whole and cannot separate the tool part, so the tools schema must follow the overall schema. It cannot be placed independently at the bottom. When the overall schema is lengthy, this actually loses a lot of potential for caching the schema. This is because, for BAML to achieve the effect of easily organizing prompts, it needs to migrate a large amount of prompts from free-form strings to the @description fields that follow the schema.
c
Ya...there is no option like
ctx.tools
Here is a video by boundary that goes deep into the topic

https://www.youtube.com/watch?v=P5wRLKF4bt8â–¾

https://github.com/ai-that-works/ai-that-works/tree/main/2025-05-27-mcp-with-10000-tools You can create tools dynamically using type builder and select the appropriate tool using various method like embedding etc, not just LLM to narrow your selection. The prompt could be something like below:
Copy code
function PickAction(state: string) -> Actions | HumanMessage {
  client "openai/gpt-4o"
  prompt #"

    Help the user by picking an action for the following.

    {{ _.role('user') }}
    {{ state }}

You are an agent with access to any number of tools.

    {{ ctx.output_format }}
  "#
}
Hope this helps
v
Yes, I have watched all the videos in the "AI That Work" series.
I think the current best practice is not to need so many tools. Instead, we should utilize the file system and bash to integrate different CLI tools. The server-side tools can access via curl. This design and debugging process will be much easier. The tool might be more suitable for sensitive permissions.
2 Views