# Agent Framework

> Give a Microsoft Agent Framework agent Corfedo's code tools, or use Corfedo AI as its chat client, in .NET or Python.

Source: https://docs.corfedo.ai/connect/frameworks/agent-framework/

Corfedo works with Microsoft Agent Framework in .NET through [`Corfedo.Extensions.AI`](https://www.nuget.org/packages/Corfedo.Extensions.AI) and in Python through the `agent-framework` extra of [`corfedo`](https://pypi.org/project/corfedo/). Your agent gets Corfedo's read tools, or Corfedo AI answers as the agent's chat client.

## Before you begin

- An API key file with **Search and read code**. Add **Ask Corfedo AI** to use Corfedo AI as the chat client. See [API keys](https://docs.corfedo.ai/connect/authentication/#api-keys).
- The packages:

**.NET**

```sh
dotnet add package Corfedo.Client
dotnet add package Corfedo.Extensions.AI
dotnet add package Microsoft.Agents.AI
```

.NET 10 and `Microsoft.Agents.AI` 1.23 or later.

**Python**

```sh
pip install 'corfedo[agent-framework]'
```

Python 3.11 or later and `agent-framework-core` 1.26 or later.

## Give your agent Corfedo's tools

**.NET**

```csharp
using Corfedo.Client;
using Corfedo.Extensions.AI;
using Microsoft.Agents.AI;

using var corfedo = await CorfedoClient.FromApiKeyFileAsync(
    Environment.GetEnvironmentVariable("CORFEDO_API_KEY_FILE")!);
var agent = new ChatClientAgent(existingModel, tools: [.. corfedo.GetAITools()]);
var answer = await agent.RunAsync("Where is the retry policy configured?");
```

`existingModel` is the `IChatClient` of your model provider. The agent runs the tool calls itself, so do not add `UseFunctionInvocation` to it.

**Python**

```python
import os
from agent_framework import Agent
from corfedo import CorfedoClient
from corfedo.agent_framework import get_corfedo_tools

async with CorfedoClient.from_api_key_file(os.environ['CORFEDO_API_KEY_FILE']) as client:
    agent = Agent(client=your_chat_client, tools=get_corfedo_tools(client))
    answer = await agent.run('Where is the retry policy configured?')
```

`your_chat_client` is the Agent Framework chat client of your model provider.

You own the model, its instructions and the agent. Each tool returns Corfedo's result as JSON text. To pick single tools, pass `new() { Groups = CorfedoAIToolGroups.None, Tools = ["search_code", "read_file"] }` to `GetAITools` in .NET, or `groups=[], tools=['search_code', 'read_file']` to `get_corfedo_tools` in Python.

Add `CorfedoAITools.Instructions` in .NET or `corfedo.tools.INSTRUCTIONS` in Python to the agent's instructions, so the model knows that a run of `*` in returned code is a masked credential, not a defect.

**Check:** the agent's tool calls name `search_code` or `read_file`, and its answer cites files from your repositories.

## Use Corfedo AI as the chat client

**.NET**

```csharp
var agent = new ChatClientAgent(new CorfedoChatClient(corfedo));
var session = await agent.CreateSessionAsync();
var first = await agent.RunAsync("How are search requests routed?", session);
var next = await agent.RunAsync("Which modules call that code?", session);
```

Set Corfedo options in `ChatClientAgentOptions.ChatOptions` as described in [Microsoft.Extensions.AI](https://docs.corfedo.ai/connect/frameworks/microsoft-extensions-ai/#use-corfedo-ai-as-the-model).

**Python**

```python
from agent_framework import Agent
from corfedo.agent_framework import CorfedoChatClient, CorfedoChatClientOptions

agent = Agent(client=CorfedoChatClient(client))
session = agent.create_session()
first = await agent.run(
    'How are search requests routed?',
    session=session,
    options=CorfedoChatClientOptions(corfedo_tools=['search_code']),
)
next_answer = await agent.run('Which modules call that code?', session=session)
```

Corfedo options are `corfedo_sources`, `corfedo_references`, `corfedo_limits` and `corfedo_tools`. Set them per run in `options`, or for every run in the agent's `default_options`. `response_format` asks for an answer that follows a Pydantic model or a JSON schema. The answer message carries the conversation and run IDs, the outcome and the citations in `additional_properties['corfedo']`.

The conversation lives in Corfedo, and the session continues it. Leave the agent's instructions empty. The [profile chosen for the API](https://docs.corfedo.ai/connect/api/ai/profiles/) answers with its own instructions. Tools you give the agent are client tools it runs when Corfedo AI asks for them.

Cancelling an agent run ends its subscription, not the Corfedo AI run. To stop that, call `CancelRunAsync` on the `CorfedoChatClient` in .NET or `client.ai.cancel` in Python with the run ID. In Python, the answer's `response_id` is the run ID.

**Check:** the second answer refers to the first, which shows the conversation continued.

## Troubleshooting

| What you see | Why | What to do |
| --- | --- | --- |
| `CorfedoUnsupportedInputException` or `CorfedoUnsupportedInputError` | The agent has instructions, or the run has a system message, non-text content, a generation setting or a tool choice other than auto. The profile owns instructions and settings. | Remove it, or change the profile |
| `CorfedoRunException` or `CorfedoRunError` | The run ended without an answer | Read its outcome |
| `CorfedoOutputDiscardedException` or `CorfedoOutputDiscardedError` | Text already streamed was replaced after the run recovered | Read the final answer with `client.Ai.GetRunAsync` in .NET or `client.ai.get_run` in Python, using the run ID from the error |
| `MiddlewareFailure` from a Corfedo tool in Python | A Corfedo tool failed for a reason the model can't fix, such as an expired API key. The `CorfedoToolError` in its cause says why. | Fix the key or the connection and run the agent again |
| `AccessDenied` | The key lacks **Ask Corfedo AI**, or Corfedo AI is off for API clients | See [Request access to Corfedo AI](https://docs.corfedo.ai/connect/authentication/#request-access-to-corfedo-ai) |

## Related

- [Microsoft.Extensions.AI](https://docs.corfedo.ai/connect/frameworks/microsoft-extensions-ai/) covers the .NET package and its options.
- [.NET SDK](https://docs.corfedo.ai/connect/sdks/dotnet/) and [Python SDK](https://docs.corfedo.ai/connect/sdks/python/) for the core clients.
- [Client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/) covers tools your application runs.
