CogniChain 0.3.0

dotnet add package CogniChain --version 0.3.0
                    
NuGet\Install-Package CogniChain -Version 0.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CogniChain" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CogniChain" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="CogniChain" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CogniChain --version 0.3.0
                    
#r "nuget: CogniChain, 0.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CogniChain@0.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CogniChain&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=CogniChain&version=0.3.0
                    
Install as a Cake Tool

CogniChain

CI NuGet License: MIT DevDad.net

Part of the DevDad.net toolbox โ€” professional engineering, shaped by real life.

A typed, composable chain layer for Microsoft.Extensions.AI's IChatClient. CogniChain sits in the gap between a raw one-shot chat call and a full Microsoft Agent Framework workflow: type-safe steps, structured output, tool calling, resilient middleware, and real token streaming โ€” all delegated to the platform, none of it reinvented.

Why CogniChain?

  • ๐ŸŽฏ Typed steps โ€” .Prompt<T>() composes with .Then<T>(), .Map(), and .Branch(); the compiler checks that adjacent steps line up.
  • ๐Ÿ“ฆ Structured output โ€” .Prompt<T>() deserializes the model's response straight into your type via JSON schema.
  • ๐Ÿ› ๏ธ Real tool calling โ€” .WithTools() / .WithToolsFrom() hand AIFunctions to the model; it decides whether to call them.
  • ๐Ÿ’พ Working conversation history โ€” reuse a ChainContext across calls and the system message and prior turns actually reach the model.
  • ๐Ÿ”„ Resilient by default โ€” RetryingChatClient classifies transient failures correctly, honors Retry-After, and never retries cancellation.
  • ๐Ÿ“ก Real streaming โ€” RunStreamingAsync yields tokens as they arrive, not once per step.
  • ๐Ÿค– Agent Framework bridge โ€” the optional CogniChain.Agents package turns a chain into an AIAgent, or drops an AIAgent into a chain.

Installation

dotnet add package CogniChain
dotnet add package CogniChain.Agents   # optional: Microsoft Agent Framework bridge

Requirements: .NET 10.0+, and any IChatClient โ€” OpenAI, Azure OpenAI, or anything else Microsoft.Extensions.AI supports.

Quick start

using CogniChain;
using Microsoft.Extensions.AI;
using OpenAI;

IChatClient chatClient = new OpenAIClient(apiKey)
    .GetChatClient("gpt-5-mini")
    .AsIChatClient()
    .AsBuilder()
    .UseFunctionInvocation()
    .Build();

var chain = Chain.Create(chatClient)
    .WithSystemMessage("You are a helpful coding assistant.")
    .Prompt("Explain {concept} in one sentence for a C# developer.")
    .Build();

var result = await chain.RunAsync(new { concept = "dependency injection" });
Console.WriteLine(result.Value);

Structured output

public sealed record MovieSuggestion(string Title, int Year, string Reason);

var chain = Chain.Create(chatClient)
    .Prompt<MovieSuggestion>("Suggest one movie about {theme}.")
    .Build();

var result = await chain.RunAsync(new { theme = "time travel" });
Console.WriteLine($"{result.Value.Title} ({result.Value.Year})");

Multi-step pipelines and tools

var chain = Chain.Create(chatClient)
    .WithTools(AIFunctionFactory.Create(GetWeather))
    .Prompt<Outline>("Outline an article about {topic}.")
    .Then<Article>(async (outline, context, ct) =>
    {
        var response = await context.ChatClient.GetResponseAsync(
            $"Write it: {string.Join(", ", outline.Sections)}", context.Options, ct);
        return new Article(outline.Sections[0], response.Text);
    })
    .Build();

See docs/getting-started.md for a full walkthrough, and examples/ for runnable OpenAI, Azure OpenAI, and Agent Framework projects.

Core concepts

Concept What it does
Chain<TIn, TOut> / ChainBuilder Composes typed steps; .Build() produces an immutable, reusable pipeline.
PromptTemplate {placeholder} rendering with {{ }} escaping, so JSON-bearing prompts work.
ChainContext Per-run state: chat client, message history, options, usage, logger. Reuse it across calls for multi-turn conversations.
MessageCountReducer Bounds conversation history, always preserving system messages.
RetryingChatClient IChatClient middleware: capped exponential backoff with full jitter, correct transient-failure classification.
ChainActivitySource Per-step OpenTelemetry spans, alongside UseOpenTelemetry() on the chat client itself.

Relationship to the rest of the .NET AI stack

CogniChain doesn't compete with Microsoft.Extensions.AI or the Microsoft Agent Framework โ€” it's a thin layer on top of the first and interoperable with the second:

  • Microsoft.Extensions.AI is the provider abstraction (IChatClient, tools, middleware). CogniChain composes chains out of IChatClient; it never implements a provider itself.
  • Microsoft Agent Framework (Microsoft.Agents.AI) is the successor to Semantic Kernel's and AutoGen's agent abstractions, for multi-agent orchestration. CogniChain.Agents bridges the two: chain.AsAIAgent() and agent.AsChainStep().
  • Semantic Kernel is in maintenance mode. If you're on it today, CogniChain slots in the same place Agent Framework does โ€” as a typed layer above IChatClient โ€” and doesn't require migrating off SK first.

Upgrading from CogniChain 0.x? See docs/migration.md.

Documentation

Community & support

License

MIT License โ€” see LICENSE.


Built with โค๏ธ for the .NET community, by DevDad.net | Star on GitHub โญ

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 195 8/23/2026
0.2.1 187 1/29/2026
0.1.0 158 1/6/2026