> ## Documentation Index
> Fetch the complete documentation index at: https://docs.deepinfra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Microsoft Agent Framework

> Run Microsoft Agent Framework agents on DeepInfra models in Python and .NET through the OpenAI-compatible API.

[Microsoft Agent Framework](https://learn.microsoft.com/agent-framework/) is Microsoft's open-source SDK for building AI agents and multi-agent workflows in Python and .NET. Its OpenAI clients work with any OpenAI-compatible endpoint, so you can point them at DeepInfra and use any [LLM from our catalog](https://deepinfra.com/models/text-generation). There is no DeepInfra-specific package; you use the standard OpenAI Chat Completions client with a custom base URL.

## Installation

```bash Python theme={null}
pip install agent-framework-openai
```

```bash .NET theme={null}
dotnet add package Microsoft.Agents.AI.OpenAI
```

Get your API key from the [Dashboard](https://deepinfra.com/dash/keys) and export it as `DEEPINFRA_API_KEY`.

## Configuration

Create the Chat Completions client with DeepInfra's base URL and your API key, then build an agent from it. Everything else works as described in the [Agent Framework docs](https://learn.microsoft.com/agent-framework/user-guide/overview).

```python Python theme={null}
import asyncio
import os

from agent_framework import Agent
from agent_framework.openai import OpenAIChatCompletionClient


async def main() -> None:
    client = OpenAIChatCompletionClient(
        model="deepseek-ai/DeepSeek-V4-Flash-0731",
        api_key=os.environ["DEEPINFRA_API_KEY"],
        base_url="https://api.deepinfra.com/v1/openai",
    )
    agent = Agent(client=client, instructions="You are a concise, helpful assistant.")

    result = await agent.run("Explain what a GPU inference provider does in two sentences.")
    print(result.text)

    async for chunk in agent.run("Write a haiku about fast inference.", stream=True):
        if chunk.text:
            print(chunk.text, end="", flush=True)
    print()


if __name__ == "__main__":
    asyncio.run(main())
```

```csharp .NET theme={null}
using System.ClientModel;
using Microsoft.Agents.AI;
using OpenAI;

var apiKey = Environment.GetEnvironmentVariable("DEEPINFRA_API_KEY")
    ?? throw new InvalidOperationException("DEEPINFRA_API_KEY is not set.");

AIAgent agent = new OpenAIClient(
        new ApiKeyCredential(apiKey),
        new OpenAIClientOptions { Endpoint = new Uri("https://api.deepinfra.com/v1/openai") })
    .GetChatClient("deepseek-ai/DeepSeek-V4-Flash-0731")
    .AsAIAgent(instructions: "You are a concise, helpful assistant.");

Console.WriteLine(await agent.RunAsync("Explain what a GPU inference provider does in two sentences."));

await foreach (AgentResponseUpdate update in agent.RunStreamingAsync("Write a haiku about fast inference."))
{
    Console.Write(update);
}
Console.WriteLine();
```

> [!WARNING]
>
> Use the Chat Completions client: `OpenAIChatCompletionClient` in Python, `GetChatClient()` in .NET. `OpenAIChatClient` (Python) and `GetResponsesClient()` (.NET) target the OpenAI Responses API, which DeepInfra serves [without server-side state](https://docs.deepinfra.com/apis/responses.md). Those clients chain tool calls and sessions through `previous_response_id` by default, and such requests fail with a 400. In Python, `default_options={"store": False}` on the agent makes `OpenAIChatClient` send the full history instead.

Function tools, sessions, and structured output need no DeepInfra-specific setup. `response_format` with `json_schema` and `strict: true` is supported on [many models](https://deepinfra.com/models?q=json); see [Structured Outputs](https://docs.deepinfra.com/chat/structured-outputs.md).

> [!NOTE]
>
> To pick a model, list the catalog with context windows and pricing via [`/v1/openai/models?filter=with_meta&sort_by=maf`](https://api.deepinfra.com/v1/openai/models?filter=with_meta\&sort_by=maf). Each entry's `metadata` block has `context_length`, `max_tokens`, and per-million-token `pricing`; the `id` is what you pass as `model`.
>
> ```bash theme={null}
> curl -s "https://api.deepinfra.com/v1/openai/models?filter=with_meta&sort_by=maf" \
>   | jq '.data[] | select(.metadata.tags | index("chat")) | {id, context_length: .metadata.context_length, pricing: .metadata.pricing}'
> ```

### Environment variables

The Python clients fall back to `OPENAI_API_KEY`, `OPENAI_BASE_URL`, and `OPENAI_CHAT_COMPLETION_MODEL` when constructor arguments are omitted, so you can also configure them that way:

```bash theme={null}
export OPENAI_API_KEY="<your DeepInfra API key>"
export OPENAI_BASE_URL="https://api.deepinfra.com/v1/openai"
export OPENAI_CHAT_COMPLETION_MODEL="deepseek-ai/DeepSeek-V4-Flash-0731"
```

> [!NOTE]
>
> If a real OpenAI key is already set in `OPENAI_API_KEY`, the client will send it to DeepInfra. Passing `api_key` and `base_url` explicitly, as above, avoids that.

## Embeddings

`OpenAIEmbeddingClient` takes the same arguments. Use any [embedding model](https://deepinfra.com/models/embeddings) from the catalog.

```python theme={null}
from agent_framework.openai import OpenAIEmbeddingClient

client = OpenAIEmbeddingClient(
    model="Qwen/Qwen3-Embedding-8B",
    api_key=os.environ["DEEPINFRA_API_KEY"],
    base_url="https://api.deepinfra.com/v1/openai",
)
result = await client.get_embeddings(["DeepInfra serves open models.", "Agents call tools."])
for embedding in result:
    print(embedding.dimensions, embedding.vector[:5])
```

## Reasoning models

DeepInfra returns the chain-of-thought of [reasoning models](https://docs.deepinfra.com/chat/reasoning.md) in a `reasoning_content` field, in both the final message and each streaming delta. Agent Framework does not read that field natively, but its `response_parser` hook lets you surface it as `text_reasoning` content. Set `reasoning_effort` through `options`; any key there is forwarded to the OpenAI SDK, and DeepInfra-only parameters can go in `options={"extra_body": {...}}`.

```python theme={null}
from typing import Any

from agent_framework import Agent, Content
from agent_framework.openai import OpenAIChatCompletionClient


def deepinfra_reasoning_parser(message: Any, contents: list[Content]) -> list[Content]:
    reasoning = getattr(message, "reasoning_content", None)
    if isinstance(reasoning, str) and reasoning:
        return [Content.from_text_reasoning(text=reasoning), *contents]
    return contents


agent = Agent(
    client=OpenAIChatCompletionClient(
        model="deepseek-ai/DeepSeek-V4-Flash-0731",
        api_key=os.environ["DEEPINFRA_API_KEY"],
        base_url="https://api.deepinfra.com/v1/openai",
        response_parser=deepinfra_reasoning_parser,
    ),
    instructions="You are a helpful assistant.",
)

result = await agent.run("What is 17 * 23?", options={"reasoning_effort": "high"})
for message in result.messages:
    for content in message.contents:
        if content.type == "text_reasoning":
            print("[reasoning]", content.text)
print("[answer]", result.text)
```

DeepInfra does not require reasoning to be echoed back on later turns, so no `message_preparer` is needed; sessions and tool-call loops work with this parser as-is.

## Learn more

> [card] [Agent Framework docs](https://learn.microsoft.com/agent-framework/user-guide/overview)
>
> Agents, tools, sessions, structured output, workflows, and observability.

> [card] [OpenAI-compatible endpoints](https://github.com/microsoft/agent-framework/blob/main/python/packages/openai/AGENTS.md)
>
> Agent Framework's guidance on adapting its OpenAI client to other providers.

> [!NOTE]
>
> Agent Framework is under active development and API names change between releases; see the [Python changelog](https://github.com/microsoft/agent-framework/blob/main/python/CHANGELOG.md). Tested with `agent-framework-openai` 1.14.2, `openai` 3.11.0, and `Microsoft.Agents.AI.OpenAI` 1.20.0.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.