Agent Framework

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

Corfedo works with Microsoft Agent Framework in .NET through Corfedo.Extensions.AI and in Python through the agent-framework extra of 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.
  • The packages:
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.

Give your agent Corfedo's tools

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.

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

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.

The conversation lives in Corfedo, and the session continues it. Leave the agent's instructions empty. The profile chosen for the API 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 seeWhyWhat to do
CorfedoUnsupportedInputException or CorfedoUnsupportedInputErrorThe 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 CorfedoRunErrorThe run ended without an answerRead its outcome
CorfedoOutputDiscardedException or CorfedoOutputDiscardedErrorText already streamed was replaced after the run recoveredRead 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 PythonA 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
AccessDeniedThe key lacks Ask Corfedo AI, or Corfedo AI is off for API clientsSee Request access to Corfedo AI

Privacy policy for this site

On this page