Back to Mastra

DInference | Models

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

2025-12-182.6 KB
Original Source

DInference

Access 3 DInference models through Mastra's model router. Authentication is handled automatically using the DINFERENCE_API_KEY environment variable.

Learn more in the DInference documentation.

bash
DINFERENCE_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: "dinference/glm-4.7"
});

// 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 DInference documentation for details.

:::

Models

<ProviderModelsTable models={[ { "model": "dinference/glm-4.7", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 128000, "inputCost": 0.45, "outputCost": 1.65 }, { "model": "dinference/glm-5", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": true, "contextWindow": 200000, "maxOutput": 128000, "inputCost": 0.75, "outputCost": 2.4 }, { "model": "dinference/gpt-oss-120b", "imageInput": false, "audioInput": false, "videoInput": false, "toolUsage": true, "reasoning": false, "contextWindow": 131072, "maxOutput": 32768, "inputCost": 0.0675, "outputCost": 0.27 } ]} />

Advanced configuration

Custom headers

typescript
const agent = new Agent({
  id: "custom-agent",
  name: "custom-agent",
  model: {
    url: "https://api.dinference.com/v1",
    id: "dinference/glm-4.7",
    apiKey: process.env.DINFERENCE_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
      ? "dinference/gpt-oss-120b"
      : "dinference/glm-4.7";
  }
});