Skip to main content
Back to Integrations Directory
FrameworkTypeScript Agent Framework

LLMSlim + Mastra

Optimize prompt contexts inside Mastra TypeScript workflow tools.

Compress tool inputs, agent system instructions, and RAG contexts inside Mastra AI applications.

1. Package Installation

Install LLMSlim and the official Mastra SDK using your package manager:

terminal
npm install @llmslim/core @mastra/core

2. Architecture & Execution Flow

STEP 01

1. Mastra workflow step fetches raw unstructured context.

STEP 02

2. Execute @llmslim/core compress() inside step handler.

STEP 03

3. Pass compressed string payload into Mastra Agent execution.

3. Production Code Pattern

Complete, runnable implementation wrapper for Mastra:

mastra-workflow.ts
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;
}

4. Production Deployment Best Practices

Import @llmslim/core in Mastra workflows, tools, and custom step execution functions.

5. Key Optimization Tips

  • 01.Pre-compress document contexts in workflow step handlers before agent generation steps.

6. Performance Metrics & Benchmark Matrix

Metric DimensionUncompressed PayloadLLMSlim CompressedRecorded Impact
Mastra Agent Payload4,000 tokens1,600 tokens60% Billed Token Reduction

7. Frequently Asked Questions

Can @llmslim/core be used inside Mastra tool definitions?

Yes. Simply invoke compress() inside your tool execute() block.

8. Troubleshooting & Diagnostics

Issue: TypeScript build error on compress import.
Solution: Ensure tsconfig.json moduleResolution is set to 'bundler' or 'node16'.