Optimize prompt contexts inside Mastra TypeScript workflow tools.
Compress tool inputs, agent system instructions, and RAG contexts inside Mastra AI applications.
Install LLMSlim and the official Mastra SDK using your package manager:
npm install @llmslim/core @mastra/core1. Mastra workflow step fetches raw unstructured context.
2. Execute @llmslim/core compress() inside step handler.
3. Pass compressed string payload into Mastra Agent execution.
Complete, runnable implementation wrapper for Mastra:
import { Agent } from "@mastra/core";
import { compress } from "@llmslim/core";
const agent = new Agent({
name: "ResearchAgent",
instructions: "You extract enterprise data insights.",
model: { provider: "OPEN_AI", name: "gpt-4o" },
});
export async function executeTask(rawContext: string, query: string) {
// Compress input context before passing to Mastra Agent
const slim = compress(rawContext, { targetRatio: 0.4 });
const response = await agent.generate([
{ role: "user", content: `Context:\n${slim.compressedText}\n\nQuery: ${query}` }
]);
return response.text;
}| Metric Dimension | Uncompressed Payload | LLMSlim Compressed | Recorded Impact |
|---|---|---|---|
| Mastra Agent Payload | 4,000 tokens | 1,600 tokens | 60% Billed Token Reduction |
Yes. Simply invoke compress() inside your tool execute() block.