---
title: "Use an existing IChatClient as a OneLLM provider in C#"
url: https://recursiveai.org/onellm/cookbook/use-an-existing-chat-client-as-a-provider/
description: "Register an existing IChatClient as a OneLLM provider and call a model from a local catalog in C#. Run the example without a service account or API key."
version: "1.0.1"
---

# Use an existing IChatClient as a OneLLM provider in C#

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm.Extensions.AI) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/use-an-existing-chat-client-as-a-provider/use-an-existing-chat-client-as-a-provider.zip)

Call `AddChatClient` with a provider name that matches your local model catalog. The demo needs no API key.

```shell
dotnet add package RecursiveAI.OneLlm.Extensions.AI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using Microsoft.Extensions.AI;
using OneLlm;
using OneLlm.Catalog;
using OneLlm.Extensions.AI;
using OneLlm.Testing;

using IChatClient client = DemoClient();
var runtime = new Runtime
    { Catalog = LocalCatalog() };
runtime.Providers.AddChatClient(
    "local", client);
var reply = await runtime.AskAsync(
    new AskRequest { Model = "demo-chat",
        Prompt = "Status?" });
Console.WriteLine(
    $"{reply.Model}: {reply.Text}");

static IChatClient DemoClient()
{
    var script = new ScriptedProvider();
    script.EnqueueText("Ready.");
    return new OneLlmChatClient(
        script, "demo-chat", TimeProvider.System);
}

static ModelCatalog LocalCatalog()
{
    var layer = CatalogLayer.FromYaml(
        CatalogLayer.User, """
        name: local
        models:
          - name: demo-chat
            contextWindow: 4096
        """);
    return ModelCatalog.Create([layer]);
}
```

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onellm/cookbook/use-an-existing-chat-client-as-a-provider/use-an-existing-chat-client-as-a-provider.zip) and `dotnet run`.

The Extensions.AI package brings in the `IChatClient` interface and `RecursiveAI.OneLlm`, which ships `ScriptedProvider` in the `OneLlm.Testing` namespace.

```text
local:demo-chat: Ready.
```

Replace `DemoClient()` with your own configured client. If the model doesn’t resolve in the catalog, OneLLM throws `ModelNotFoundException` before calling the chat client.

## Variations

**Qualify the model with the provider name.** Change the model in the request to select `local` explicitly. Keep the `name: demo-chat` entry unchanged:

```csharp
var reply = await runtime.AskAsync(new AskRequest
    { Model = "local:demo-chat", Prompt = "Status?" });
Console.WriteLine($"{reply.Model}: {reply.Text}");
```

```text
local:demo-chat: Ready.
```

## Related recipes

- [Mock an IChatClient with a scripted provider](https://recursiveai.org/onellm/cookbook/mock-ichatclient-with-scripted-provider/)
- [Stream an LLM response in C#](https://recursiveai.org/onellm/cookbook/stream-chat-response/)

## The short answer

Install the OneLLM Extensions.AI bridge and call `runtime.Providers.AddChatClient("local", client)`. Define the model under `name: local` in the runtime’s catalog, then ask for that model through `runtime.AskAsync`. The example prints `local:demo-chat` before the reply so you can see which model route it used.

## How do you use an IChatClient as a provider in .NET?

Use the client you already configured in your application. `IChatClient` is the Microsoft.Extensions.AI chat interface; the OneLLM bridge translates the prompt and options into a client request and returns the resulting message as a OneLLM answer. The demo uses a `OneLlmChatClient` with a scripted response only to supply a working `IChatClient` with no service. Your client doesn’t need a scripted provider or a OneLLM client wrapper.

Register at setup, not once per chat turn. `AddChatClient` doesn’t take ownership of the client, so keep the client alive while the runtime needs it and let your application dispose of it. If you already have an embedding generator, you can pass it separately; a chat client alone doesn’t supply embeddings. See [using a client as a provider](https://recursiveai.org/onellm/docs/extensions-ai/#a-client-as-a-provider).

## How does OneLLM find the model for an existing chat client?

The catalog and the registry do different jobs. The catalog resolves `demo-chat` to the provider called `local`; it also records information such as the context window. The registry maps `local` to the client you passed to `AddChatClient`. Registering a client by itself doesn’t add models to the catalog or query the underlying service for a list. Use the model name your configured client understands, and if two providers advertise the same name, qualify it as `provider:model` to choose one explicitly.
