Back to Mastra

Hugging Face | Models

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

2025-12-187.4 KB
Original Source

Hugging Face

Access 20 Hugging Face models through Mastra's model router. Authentication is handled automatically using the HF_TOKEN environment variable.

Learn more in the Hugging Face documentation.

bash
HF_TOKEN=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: "huggingface/MiniMaxAI/MiniMax-M2.1"
});

// 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);
}

:::info

Mastra uses the OpenAI-compatible /chat/completions endpoint. Some provider-specific features may not be available. Check the Hugging Face documentation for details.

:::

Models

<ProviderModelsTable models={[ { "model": "huggingface/deepseek-ai/DeepSeek-R1-0528", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 163840, "maxOutput": 163840, "inputCost": 3, "outputCost": 5 }, { "model": "huggingface/deepseek-ai/DeepSeek-V3.2", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 163840, "maxOutput": 65536, "inputCost": 0.28, "outputCost": 0.4 }, { "model": "huggingface/MiniMaxAI/MiniMax-M2.1", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 204800, "maxOutput": 131072, "inputCost": 0.3, "outputCost": 1.2 }, { "model": "huggingface/MiniMaxAI/MiniMax-M2.5", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 204800, "maxOutput": 131072, "inputCost": 0.3, "outputCost": 1.2 }, { "model": "huggingface/moonshotai/Kimi-K2-Instruct", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 16384, "inputCost": 1, "outputCost": 3 }, { "model": "huggingface/moonshotai/Kimi-K2-Instruct-0905", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 16384, "inputCost": 1, "outputCost": 3 }, { "model": "huggingface/moonshotai/Kimi-K2-Thinking", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 262144, "maxOutput": 262144, "inputCost": 0.6, "outputCost": 2.5 }, { "model": "huggingface/moonshotai/Kimi-K2.5", "imageInput": true, "audioInput": false, "videoInput": true, "toolUsage": true, "reasoning": true, "contextWindow": 262144, "maxOutput": 262144, "inputCost": 0.6, "outputCost": 3 }, { "model": "huggingface/Qwen/Qwen3-235B-A22B-Thinking-2507", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 262144, "maxOutput": 131072, "inputCost": 0.3, "outputCost": 3 }, { "model": "huggingface/Qwen/Qwen3-Coder-480B-A35B-Instruct", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 66536, "inputCost": 2, "outputCost": 2 }, { "model": "huggingface/Qwen/Qwen3-Coder-Next", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 65536, "inputCost": 0.2, "outputCost": 1.5 }, { "model": "huggingface/Qwen/Qwen3-Embedding-4B", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": false, "contextWindow": 32000, "maxOutput": 2048, "inputCost": 0.01, "outputCost": null }, { "model": "huggingface/Qwen/Qwen3-Embedding-8B", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": false, "reasoning": false, "contextWindow": 32000, "maxOutput": 4096, "inputCost": 0.01, "outputCost": null }, { "model": "huggingface/Qwen/Qwen3-Next-80B-A3B-Instruct", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 66536, "inputCost": 0.25, "outputCost": 1 }, { "model": "huggingface/Qwen/Qwen3-Next-80B-A3B-Thinking", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 262144, "maxOutput": 131072, "inputCost": 0.3, "outputCost": 2 }, { "model": "huggingface/Qwen/Qwen3.5-397B-A17B", "imageInput": true, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 262144, "maxOutput": 32768, "inputCost": 0.6, "outputCost": 3.6 }, { "model": "huggingface/XiaomiMiMo/MiMo-V2-Flash", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 262144, "maxOutput": 4096, "inputCost": 0.1, "outputCost": 0.3 }, { "model": "huggingface/zai-org/GLM-4.7", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 204800, "maxOutput": 131072, "inputCost": 0.6, "outputCost": 2.2 }, { "model": "huggingface/zai-org/GLM-4.7-Flash", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 128000, "inputCost": null, "outputCost": null }, { "model": "huggingface/zai-org/GLM-5", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 202752, "maxOutput": 131072, "inputCost": 1, "outputCost": 3.2 } ]} />

Advanced configuration

Custom headers

typescript
const agent = new Agent({
  id: "custom-agent",
  name: "custom-agent",
  model: {
    url: "https://router.huggingface.co/v1",
    id: "huggingface/MiniMaxAI/MiniMax-M2.1",
    apiKey: process.env.HF_TOKEN,
    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
      ? "huggingface/zai-org/GLM-5"
      : "huggingface/MiniMaxAI/MiniMax-M2.1";
  }
});