CogniChain 0.3.0
dotnet add package CogniChain --version 0.3.0
NuGet\Install-Package CogniChain -Version 0.3.0
<PackageReference Include="CogniChain" Version="0.3.0" />
<PackageVersion Include="CogniChain" Version="0.3.0" />
<PackageReference Include="CogniChain" />
paket add CogniChain --version 0.3.0
#r "nuget: CogniChain, 0.3.0"
#:package CogniChain@0.3.0
#addin nuget:?package=CogniChain&version=0.3.0
#tool nuget:?package=CogniChain&version=0.3.0
CogniChain
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()handAIFunctions to the model; it decides whether to call them. - ๐พ Working conversation history โ reuse a
ChainContextacross calls and the system message and prior turns actually reach the model. - ๐ Resilient by default โ
RetryingChatClientclassifies transient failures correctly, honorsRetry-After, and never retries cancellation. - ๐ก Real streaming โ
RunStreamingAsyncyields tokens as they arrive, not once per step. - ๐ค Agent Framework bridge โ the optional
CogniChain.Agentspackage turns a chain into anAIAgent, or drops anAIAgentinto 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.AIis the provider abstraction (IChatClient, tools, middleware). CogniChain composes chains out ofIChatClient; 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.Agentsbridges the two:chain.AsAIAgent()andagent.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
- ๐ Getting Started
- ๐ API Reference
- ๐ Best Practices
- ๐๏ธ Architecture
- ๐ Migration from 0.x
- ๐ก Examples โ OpenAI, Azure OpenAI, Agent Framework
Community & support
- ๐ฌ GitHub Discussions
- ๐ Issue Tracker
- ๐ค Contributing ยท ๐ Security ยท ๐ Changelog
License
MIT License โ see LICENSE.
Built with โค๏ธ for the .NET community, by DevDad.net | Star on GitHub โญ
| Product | Versions 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. |
-
net10.0
- Microsoft.Extensions.AI (>= 10.9.0)
- Microsoft.Extensions.AI.Abstractions (>= 10.9.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.11)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See https://github.com/wouternijenhuis/CogniChain/blob/main/CHANGELOG.md for release notes.