Back to Mastra

Groq | Models

docs/src/content/en/models/providers/groq.mdx

2025-12-184.5 KB
Original Source

Groq

Access 9 Groq models through Mastra's model router. Authentication is handled automatically using the GROQ_API_KEY environment variable.

Learn more in the Groq documentation.

bash
GROQ_API_KEY=your-api-key
typescript
import { Agent } from "@mastra/core/agent";

const agent = new Agent({
  id: "my-agent",
  name: "My Agent",
  instructions: "You are a helpful assistant",
  model: "groq/llama-3.1-8b-instant"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
  console.log(chunk);
}

Models

<ProviderModelsTable models={[ { "model": "groq/llama-3.1-8b-instant", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 131072, "inputCost": 0.05, "outputCost": 0.08 }, { "model": "groq/llama-3.3-70b-versatile", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 32768, "inputCost": 0.59, "outputCost": 0.79 }, { "model": "groq/meta-llama/llama-4-maverick-17b-128e-instruct", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.2, "outputCost": 0.6 }, { "model": "groq/meta-llama/llama-4-scout-17b-16e-instruct", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 8192, "inputCost": 0.11, "outputCost": 0.34 }, { "model": "groq/meta-llama/llama-guard-4-12b", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": false, "contextWindow": 131072, "maxOutput": 1024, "inputCost": 0.2, "outputCost": 0.2 }, { "model": "groq/moonshotai/kimi-k2-instruct-0905", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 16384, "inputCost": 1, "outputCost": 3 }, { "model": "groq/openai/gpt-oss-120b", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 65536, "inputCost": 0.15, "outputCost": 0.6 }, { "model": "groq/openai/gpt-oss-20b", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 65536, "inputCost": 0.075, "outputCost": 0.3 }, { "model": "groq/qwen/qwen3-32b", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 131072, "maxOutput": 16384, "inputCost": 0.29, "outputCost": 0.59 } ]} />

Advanced configuration

Custom headers

typescript
const agent = new Agent({
  id: "custom-agent",
  name: "custom-agent",
  model: {
    url: "https://api.groq.com/openai/v1",
    id: "groq/llama-3.1-8b-instant",
    apiKey: process.env.GROQ_API_KEY,
    headers: {
      "X-Custom-Header": "value"
    }
  }
});

Dynamic model selection

typescript
const agent = new Agent({
  id: "dynamic-agent",
  name: "Dynamic Agent",
  model: ({ requestContext }) => {
    const useAdvanced = requestContext.task === "complex";
    return useAdvanced
      ? "groq/qwen/qwen3-32b"
      : "groq/llama-3.1-8b-instant";
  }
});

Direct provider installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

bash
npm install @ai-sdk/groq

For detailed provider-specific documentation, see the AI SDK Groq provider docs.