anthropic-sdk-csharp
CLAUDE.md328 starsChanged 2 months agoMITView on GitHub
Context for contributors (human or AI) on how this SDK is put together, plus the things reviews most often come back to. Setup instructions live in CONTRIBUTING.md.
The file
We split this CLAUDE.md into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 57
Not every block has deltas: some arrive complete in content_block_start (redacted_thinking, server-tool and MCP tool *results*, and new …
- Distinctive: line 65
Unverified claims about the API: Check doc comments, exception messages, and PR text that say "the API rejects X" or "model Y requires Z" …
- Very specific: line 72
Avoid @-escaped identifiers (@event → events/streamEvent).
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Working in this repository
Context for contributors (human or AI) on how this SDK is put together, plus the things reviews most often come back to. Setup instructions live in CONTRIBUTING.md.
Overview
- The official C#/.NET SDK for the Claude API. Much of it is generated from the OpenAPI spec, with hand-written helpers, adapters, credentials, and platform clients layered on top.
- Generated files are safe to edit. New generator output is git-merged with custom changes rather than overwriting them, so fix things where they live; a collision is just a merge conflict.
- Bugs that originate upstream are worth flagging as such even when a local patch lands first: a real API response the SDK's generated types can't deserialize or validate (a missing "required" field, an unknown enum member, a nullability mismatch) points at the OpenAPI spec, and broken generated infrastructure (retries, SSE parsing, serialization, disposal) points at the generator — fixing it there fixes every SDK.
- PRs target the default branch (
mainon the public repo).nextbelongs to release automation — don't open PRs against it or push to it; a commit that lands there outside the release flow breaks the sync. Rebase onto the current default branch before asking for review — a stale base shows up as dozens of unrelated "changed" files. - Package versions (
VersionPrefix,.release-please-manifest.json) and the per-packagesrc/*/CHANGELOG.mdfiles are written by release automation. What gets built, packed, and published is whatever is in the rootAnthropic.sln; a new package additionally needs entries inrelease-please-config.jsonand the manifest, plus a line in the hand-maintained rootCHANGELOG.mdindex.
Build, test, lint
- The entry points are
./scripts/{bootstrap,build,test,format,lint}(see CONTRIBUTING.md);buildcompilesAnthropic.slnandexamples/Anthropic.Examples.sln. Runformatandlintbefore pushing changes to C# or project files. - Lint is
dotnet csharpier check .(pinned version, viadotnet tool restore) plusdotnet format style/analyzersat info severity, so even IDE/CA suggestions fail it; CI, not a local run, is the source of truth. Prefer fixing a finding over suppressing it, and comment any suppression that's genuinely needed. .csfiles use CRLF line endings (.editorconfig); csharpier flags LF files, which occasionally arrive via codegen, and./scripts/formatfixes them.- IDE churn (
.slnheader rewrites, addedthis.qualifiers, editor-specific.gitignoreentries, whitespace-only diffs) is best reverted before committing. - Warnings are errors and nullable reference types are on throughout
src/. Other settings (target frameworks,LangVersion,ImplicitUsings) come fromsrc/Directory.Build.propsand several projects override them —ImplicitUsings, for instance, is off inAnthropicbut on in most platform packages and the examples — so follow the project you're in rather than assuming. - The libraries multi-target modern .NET plus
netstandard2.0, so build with an SDK at least as new as the newest target framework. Tests run on .NET and onnet472; thenet472run is what exercises thenetstandard2.0build (on Windows in CI), so both sides of a#ifget tested somewhere. ./scripts/teststarts a Node-based mock API server first (./scripts/mock, port 4010, or pointTEST_API_BASE_URLelsewhere). Only the generatedServicestests and their*MultiClientTestwrappers talk to it; everything else, including the generatedModelstests, runs without it. Live tests against real cloud providers only run withANTHROPIC_LIVE=1.- CI's
./scripts/detect-breaking-changescompilesmain's generatedServices/Modelstests against your branch. It only sees the generated API surface, so a break in hand-written public API (helpers, adapters, credentials, platform clients) has to be called out by the author; and if the job fails the same way on the base branch without your changes, it's pre-existing drift awaiting release rather than something the PR did. - Example projects target
net8.0, should read credentials from the environment, and need adding toexamples/Anthropic.Examples.sln:./scripts/buildcompiles the examples through that solution, and./scripts/lintfails if a project underexamples/is missing from it. New user-facing helpers usually come with an example; user documentation lives outside this repo (see CONTRIBUTING.md), so note in the PR what needs documenting.
Multi-targeting
#if NET/#if !NETis the convention for TFM splits, wrapped around just the fragment that differs. The*_OR_GREATERsymbols are easy to misjudge, so check which targets define a symbol before relying on one.- Behaviour should be the same on every target. When an API is missing on netstandard2.0, use an equivalent that exists everywhere or add an internal shim (
src/Anthropic/Shims.cs,src/Anthropic/Core/HttpContentExtensions.cs,src/Anthropic.Bedrock/LocalShims.cs) rather than compiling the behaviour out on one target — a guard around the missingHttpStatusCode.TooManyRequestsonce silently disabled 429 retries for every netstandard2.0 consumer, where(HttpStatusCode)429would have kept them. - Polyfilled attribute/compiler types stay
internal(public copies collide with consumers' own polyfills), and types that ship as netstandard2.0 packages (System.Collections.Immutable,System.Threading.Tasks.Extensions) are referenced rather than polyfilled.required,init,[NotNullWhen]and friends compile on netstandard2.0 throughShims.cs, which the other packages pick up by compile-linking the file — a project that doesn't link it doesn't have them.
Code organization
Anthropic.Helpers(with public sub-namespaces such asAnthropic.Helpers.Betafor the tool runner) is the curated helper surface users import, so new internal plumbing shouldn't be added directly to it. Cross-cutting internals go inAnthropic.Core(namespace andsrc/Anthropic/Core/directory;JsonNodes,LazySseBody,StreamedToolInputare precedents), and internals serving one feature go in an internal sub-folder/sub-namespace next to it (Helpers/Fallbacks/*).- Public types placed in a namespace the SDK doesn't own (in practice
Microsoft.Extensions.AI, MEAI) carry anAnthropicprefix so they can't collide with types the namespace owner adds later;NamingConventionTestenforces this, withAIContentCacheExtensionsexempt because it predates the convention. - The
Betaprefix mirrors the API split:client.Beta.*services and theAnthropic.Models.Beta.*namespaces are the beta surface, and public helpers over beta-only types are namedBeta*. - A lot of hand-written logic exists twice, stable and beta (
AnthropicClientExtensions/AnthropicBetaClientExtensions,MessageContentAggregator/BetaMessageContentAggregator, …), and the platform packages duplicate plumbing too (Bedrock and Vertex request validation and URL rewriting; SigV4 signing in bothAnthropic.AwsandAnthropic.Bedrock). A fix usually needs applying to every copy. Sharing the logic through an internal helper, asStreamedToolInputdoes for the aggregators, beats maintaining parallel copies — but the stable and beta public API types themselves stay separate (no shared base types or unified models), so the two surfaces can evolve independently. - For anything cross-SDK (environment variable and option names, header semantics, credential precedence, client-side policies like the non-streaming
max_tokens/timeout guard, helper shapes such as aggregation returning the non-streamingMessagetype), matching the other SDKs is the default.
Exceptions
- Everything the SDK throws on its own behalf derives from
AnthropicException(Anthropic.Exceptions), so callers can catch the family in one clause:AnthropicServiceException— the service reported an error; carries the parsedErrorType(InvalidRequestError,RateLimitError,OverloadedError, …) when the body names one.AnthropicApiExceptionis its non-2xx form, addingStatusCodeandResponseBody, andAnthropicExceptionFactorypicks the concrete subclass by status (AnthropicBadRequestException,AnthropicUnauthorizedException,AnthropicForbiddenException,AnthropicNotFoundException,AnthropicUnprocessableEntityException,AnthropicRateLimitException, otherwiseAnthropic4xxException/Anthropic5xxException, otherwiseAnthropicUnexpectedStatusCodeException);AnthropicSseExceptionis anerrorevent arriving inside an otherwise successful stream.AnthropicIOException— a transport failure wrapping theHttpRequestException; the client counts it as retryable.AnthropicInvalidDataException— data that doesn't fit the models: JSON that won't deserialize, a failedValidate(), an absent required field, a wire shape a helper can't interpret. This is the type hand-written code throws most.- Plain
AnthropicExceptionwith an actionable message for client-side configuration problems (a platform client that can't resolve a region, workspace, or credentials), and feature-specific subclasses such asWorkloadIdentityExceptionwhere a family of failures deserves its own type.
- Deliberately outside that family: argument errors at public entry points are
ArgumentNullException/ArgumentException; operations a platform client can't offer throwNotSupportedException; cancellation and client-side timeouts surface asOperationCanceledException/TaskCanceledException; andBetaToolErrorisn't a failure but the signal a tool implementation throws so the tool runner returnsis_error: truecontent to the model. - New code reuses these rather than adding types or throwing BCL exceptions for SDK conditions, keeps the original exception as
InnerException, and leaves HTTP-status mapping toAnthropicExceptionFactory.
Common mistakes
- Rewriting the request to make it "work". Don't hoist, merge, reorder, or drop parts of a request the caller built. Map inputs as-is and let the API reject what it doesn't support, so the caller gets an actionable 400 instead of silently different semantics. (Protocol translation in the platform adaptation handlers —
modelinto the URL,anthropic_version/anthropic_betainto the body — is mapping, not rewriting.) - Inferring capabilities from model IDs. Don't branch on model-name lists or prefixes: they go stale on the next launch and don't match Bedrock/Vertex/Foundry spellings or custom deployment names. Use an explicit option, or just send the request. (The exact-match tables mirrored verbatim from the other SDKs — the thinking-enabled deprecation warning and the non-streaming token limits — are the accepted precedents.)
- Throwing on data the SDK doesn't recognize. API responses can contain enum values, block types, and fields this SDK version doesn't model. Enumerate the cases you handle and pass everything else through untouched — especially in streaming and middleware code, where an exception tears down the caller's response. Compare enums as
x.Value() == Enum.Memberorx.Raw() == "wire_value", never throughToString()(it returns JSON, quotes included), and rememberValue()is an undefined member for values the SDK doesn't know, which can't be re-serialized. - Losing wire data on the way back to the API. Models are raw-JSON-backed (
RawData,FromRawUnchecked); when relaying API output into a new request, carry the raw data across rather than rebuilding from typed properties, which drops fields such ascaller. - Sending
nullwhere the property should be omitted. Request models are immutable — build once and usewithfor conditional additions.Prop = cond ? value : nullis harmless on most optional properties (anullassignment is simply ignored there) but a trap on the ones the spec marks nullable (ContextManagement,CacheControl,Container, …): for those the assignednullis sent as"prop": null, which the API may treat differently from an omitted property. - Streaming assumptions that don't hold.
- Not every block has deltas: some arrive complete in
content_block_start(redacted_thinking, server-tool and MCP tool results, and new block types keep appearing), so default to passing whole blocks through. tool_use,server_tool_use, andmcp_tool_usestart withinput: {}and stream the real JSON asinput_json_deltafragments, which need folding back into the block and may be truncated when the stream stops early.- Per-block state is keyed and flushed by the event's
Index, not "everything accumulated so far". - Cumulative usage fields on
message_deltaare nullable and shouldn't overwritemessage_startvalues with null. - Some fields only arrive as deltas, not on the start block (a thinking
signature, for example).
- Not every block has deltas: some arrive complete in
- Clobbering headers.
anthropic-betaand the helper-telemetry header are comma-joined sets: append and de-duplicate rather than overwrite or emit a second header line (there is an existing merge helper inAnthropic.Core), and keep a singleUser-Agent. When a helper or adapter emits a feature that needs a beta flag, add it automatically, merged with the caller'sBetas. - Leaking first-party credentials or dropping client options in platform clients. The Bedrock, Vertex, Foundry, AWS, and Google Cloud clients null out the base client's environment-resolved
ApiKey/AuthTokenso those never reach a third-party host, keep scheme, host and port (Uri.Authority) when rewriting URLs, honourClientOptions(timeout, retries, headers) rather thanHttpClientdefaults, and use the header/scheme casing the provider expects; a new platform client needs all four. - Disposing things the caller still needs, and other I/O slips. Don't dispose (or
using) aCancellationTokenSource, stream, orHttpResponseMessagethat the returned object still depends on. Reads may return fewer bytes than requested, stay async, and take the caller'sCancellationToken; libraryawaits use.ConfigureAwait(false); and the only console output is the existing user-facing warnings onConsole.Error. - Unverified claims about the API. Check doc comments, exception messages, and PR text that say "the API rejects X" or "model Y requires Z" against the official Claude API docs, not a third-party page or a bug report.
Style notes
- Type safety comes first. Bypassing the type system — null-forgiving
!, casting around the compiler,object/reflection, or manipulating raw JSON where typed models exist — is occasionally necessary but usually a sign something should be restructured: pattern-match once and pass the validated value on, and capture nullable values with a pattern instead of?.followed by.Value. - Immutable things should actually be immutable. A mutable collection exposed through a read-only interface can be cast back, so wrap it (
ReadOnlyDictionary, asSseAggregatordoes) or use the frozen collections the models use; don't hand out references that let callers mutate state presented as fixed. - Public entry points guard reference arguments with
ArgumentNullException(tests pin those); past that boundary, values the signature declares non-nullable don't need re-checking. Data that turns out absent or malformed at runtime (API payloads, raw JSON, URI segments) gets a descriptiveAnthropicInvalidDataException(see Exceptions) rather than an NRE. - Avoid
@-escaped identifiers (@event→events/streamEvent). - A small constructor plus
required initproperties reads better than a long positional parameter list; when you hold the concrete variant type, the implicit conversions on unions and enums makenew(...)wrappers unnecessary. - Keyed lookups come as a pair:
TryGetXreturningnull/false, andGetXthrowingKeyNotFoundExceptionvia theTryvariant. sealedunless designed for inheritance;internalunless deliberately public (Anthropic,Anthropic.Bedrock, andAnthropic.Mcpgrant the test projectInternalsVisibleTo; add it to another project if you need it);/// <inheritdoc/>on overrides; XML docs using<c>/<see>rather than Markdown. Changing an existing public signature is a breaking change even wheredetect-breaking-changescan't see it; an added overload usually isn't.- Explicit
StringComparison(ordinal for protocol tokens); LINQ set operations over hand-rolledContainsloops;static readonlyfor shared immutable data. - New siblings follow the existing shape: aggregators get a
CollectAsyncextension likeSseAggregatorExtensions, MEAI passthroughs useAdditionalProperties[nameof(Tool.X)], per-content options useanthropic:-prefixed keys likeWithCacheControl, and MEAI values go throughAIJsonUtilities.DefaultOptions.GetTypeInfo(...)rather than reflection-based serialization. "How do we already do this elsewhere?" is usually the first review question. - Comments and XML docs are welcome where they explain something non-obvious (an invariant, a wire-format reason, a race, why a
#ifsplit or a suppression exists) and unnecessary where they restate what the code plainly does. Capitalized sentence style.
Tests
- A behaviour change comes with a test that fails before and passes after; new internal classes get unit tests of their own rather than only end-to-end coverage.
- Hand-written tests are plain xunit classes (arrange / act / assert) and don't inherit
TestBase, which is the base for the generated tests.Xunitis a global using; async SDK calls takeTestContext.Current.CancellationToken. - Generated
*ServiceTestmethods that have a[Theory] + [AnthropicTestClients]wrapper in a*MultiClientTestrun against every client flavour and have their own[Fact]removed; the rest keep[Fact]and run once against the default client, and[Fact(Skip = …)]methods stay as they are (the mock can't serve those endpoints). Codegen re-adds[Fact]to regenerated methods, andGeneratedTestConformanceTestonly prints a warning about that rather than failing, so re-strip them from wrapped services after a codegen update. - MEAI adapter tests go in
AnthropicClientExtensionsTestsBaseso they run against both adapters; only flavour-specific cases live in the derived classes. - Tests that mutate environment variables join
[Collection("EnvVarMutating")](defined with parallelization disabled), tests that redirectConsole.Errorshare[Collection("console stderr")]so they don't interleave with each other, and both restore what they change. - For request-shaping code, asserting on the exact wire request (body and headers) is what makes the test meaningful; fixtures shouldn't enshrine shapes the real API would reject. Headers captured from a real socket arrive comma-folded into one line.
Commits and pull requests
- Conventional Commits drive release-please and the changelog (
feat(scope):,fix(scope):,chore,docs,refactor, …). - A
!orBREAKING CHANGE:footer makes release-please cut a major version ofAnthropic(a minor for the pre-1.0 platform packages). Major bumps are rare and a maintainer call — a small, technically-breaking change doesn't warrant one; describe the compatibility impact in the PR instead. - One logical change per PR, against the current default branch, with formatting-only churn (line-ending fixes included) in its own commit; big mixed PRs tend to get sent back to be split.
- A good PR description says what was wrong (a short before/after helps), what changed, and what gives confidence — the failing-then-passing test plus any manual verification — and calls out behaviour changes and anything left unverified.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

