Compendium.Adapters.Anthropic 1.0.0-preview.0

This is a prerelease version of Compendium.Adapters.Anthropic.
dotnet add package Compendium.Adapters.Anthropic --version 1.0.0-preview.0
                    
NuGet\Install-Package Compendium.Adapters.Anthropic -Version 1.0.0-preview.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="Compendium.Adapters.Anthropic" Version="1.0.0-preview.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Compendium.Adapters.Anthropic" Version="1.0.0-preview.0" />
                    
Directory.Packages.props
<PackageReference Include="Compendium.Adapters.Anthropic" />
                    
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 Compendium.Adapters.Anthropic --version 1.0.0-preview.0
                    
#r "nuget: Compendium.Adapters.Anthropic, 1.0.0-preview.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 Compendium.Adapters.Anthropic@1.0.0-preview.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=Compendium.Adapters.Anthropic&version=1.0.0-preview.0&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Compendium.Adapters.Anthropic&version=1.0.0-preview.0&prerelease
                    
Install as a Cake Tool

compendium-adapter-anthropic

Anthropic (Claude) AI provider adapter for the Compendium event-sourcing framework. Implements IAIProvider from Compendium.Abstractions.AI against Anthropic's Messages API — synchronous + streaming (SSE), with typed error mapping and opt-in prompt caching.

Built from template-compendium-adapter-dotnet per ADR-0006 (one adapter, one repo).

Install

dotnet add package Compendium.Adapters.Anthropic
// From IConfiguration (binds the "Anthropic" section)
services.AddCompendiumAnthropic(builder.Configuration);

// Or inline
services.AddCompendiumAnthropic(o =>
{
    o.ApiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")!;
    o.DefaultModel = "claude-3-7-sonnet-latest";
    o.EnablePromptCaching = true;
});

Then inject IAIProvider:

public sealed class MyService(IAIProvider ai)
{
    public Task<Result<CompletionResponse>> SummariseAsync(string text, CancellationToken ct) =>
        ai.CompleteAsync(new CompletionRequest
        {
            Model = "claude-3-5-haiku-latest",
            SystemPrompt = "You are a concise summariser.",
            Messages = new List<Message> { Message.User(text) },
        }, ct);
}

A runnable end-to-end demo (sync + streaming, prompt caching enabled) lives in samples/01-chat.

Configuration

Option Default Notes
ApiKey (required) Sent as the x-api-key header. Read from a secret store — never hard-code.
BaseUrl https://api.anthropic.com Override only for proxies or contract tests.
AnthropicVersion 2023-06-01 Sent as the anthropic-version header. Bump when consuming newer beta features.
DefaultModel claude-3-7-sonnet-latest Used when CompletionRequest.Model is null/empty.
DefaultMaxTokens 4096 Anthropic requires max_tokens on every request — the adapter always sends one.
TimeoutSeconds 120 Wraps the typed HttpClient timeout.
EnableLogging false When true, raw request/response JSON is logged at Debug. Keep off in prod (prompts may carry PII).
EnablePromptCaching false When true, the adapter adds cache_control: { type: "ephemeral" } to the system-prompt block. See Prompt caching below.

Bind from configuration under the Anthropic section:

{
  "Anthropic": {
    "ApiKey": "sk-ant-...",
    "DefaultModel": "claude-3-7-sonnet-latest",
    "EnablePromptCaching": true
  }
}

Prompt caching (preview)

When EnablePromptCaching = true, the adapter emits a cache_control: { type: "ephemeral" } marker on the trailing system-prompt block of every request. Anthropic then caches that prefix for ~5 minutes (the ephemeral tier), so repeat calls with the same system prompt only pay the input-token cost for the delta (typically the user message + any tools).

This is most useful when:

  • Your system prompt is large (≥1024 tokens for Sonnet, ≥2048 for Haiku) and stable across calls.
  • You are doing high-frequency turns over the same persona/context.

Models that currently support prompt caching include the Claude 3.5/3.7/4.x Sonnet and Opus families. See Anthropic's prompt-caching docs for the authoritative list and pricing. The adapter writes the marker unconditionally when the option is on; Anthropic silently ignores it on unsupported models.

What's in scope

Surface Status
Messages API — sync (CompleteAsync)
Messages API — streaming SSE (StreamCompleteAsync) ✅ (parses message_start / content_block_delta / message_delta / message_stop)
Typed error mapping (auth / rate / payment / model-not-found / 5xx)
Cancellation tokens honoured end-to-end
Prompt caching markers (opt-in)
Curated model catalog (ListModelsAsync) ✅ (static; Anthropic's public /v1/models is not yet covered)
Health check (HealthCheckAsync) ✅ (1-token probe against the default model)
Embeddings (EmbedAsync) ❌ — Anthropic does not expose embeddings; the adapter returns AI.InvalidRequest so callers can fall back to a dedicated provider.

Out of scope (this preview)

  • Provider-native tool / function calling. IAIProvider is text-only; the Compendium agent layer (Compendium.Application.StandardAgent) implements tool use through ReAct-style prompt injection, which already works against Claude. Native tool_use content blocks are deferred until the abstraction grows a tools surface.
  • Vision / image content blocks. Same reason — Message.Content is string. Use a multimodal-aware surface (or a data: URI inlined in the message body) until the abstraction expands.
  • Extended thinking (beta-gated reasoning blocks) — deferred behind a feature flag.
  • Computer use — out of scope for v1.0.0-preview.
  • AWS Bedrock / Vertex AI hosted Claude — those will live in separate adapters.

SDK choice

Hand-rolled HttpClient + System.Text.Json, mirroring compendium-adapter-openrouter. Rationale: avoid churn from official SDK pre-1.0 surfaces, reuse the Microsoft.Extensions.Http.Resilience policies already pinned by the framework, and keep the runtime graph minimal.

Repository conventions

Aspect Choice
Target .NET 9, C# 13
Test framework xUnit 2.9.3 + FluentAssertions 6.12.1 + NSubstitute 5.1.0
HTTP mocking RichardSzalay.MockHttp 7.0.0
Result pattern Result<T> from Compendium.Core
Coverage 98.8 % line / 91.2 % branch (77 tests) — gate at 90 %
Test naming {SUT}Tests / {Method}_{Scenario}_{Expected} + AAA explicit

Build & test locally

dotnet restore
dotnet build -c Release
dotnet test  -c Release --collect:"XPlat Code Coverage"

Releasing

Tag with a v prefix on main to publish to nuget.org + GitHub Packages:

git tag v1.0.0-preview.0
git push origin v1.0.0-preview.0

See docs/RELEASE.md for the full release procedure and required secrets.

License

MIT — Copyright © 2026 Sassy Solutions.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.0.0-preview.0 33 5/17/2026