This document provides information for developers working on the Claude Code SDK for .NET.
claude-code-sdk-csharp/
├── src/ClaudeCodeSdk/ # Main SDK library
│ ├── Types/ # Type definitions and models
│ ├── Exceptions/ # Custom exception types
│ ├── Internal/ # Internal implementation
│ │ └── Transport/ # Transport layer
│ ├── ClaudeSDKClient.cs # Interactive client
│ ├── ClaudeQuery.cs # One-shot query functionality
│ └── ClaudeCodeSDK.cs # Main SDK entry point
├── examples/ # Usage examples
├── tests/ # Unit tests
├── ClaudeCodeSdk.sln # Solution file
└── README.md # Documentation
# Build the solution
dotnet build
# Run tests
dotnet test
# Pack NuGet package
dotnet pack src/ClaudeCodeSdk/ClaudeCodeSdk.csproj -c Release- Follow standard C# naming conventions
- Use nullable reference types throughout
- Prefer records for data types
- Use async/await for all I/O operations
- Include XML documentation for public APIs
The transport layer handles communication with the Claude CLI subprocess:
ITransport- Interface for transport implementationsSubprocessCliTransport- Default subprocess implementation
Messages are parsed from JSON into strongly-typed objects:
MessageParser- Converts JSON to typed messages- Message types implement
IMessage - Content blocks implement
IContentBlock
Two main interaction patterns:
-
One-shot queries via
ClaudeQuery.QueryAsync()- Simple, fire-and-forget style
- Good for batch processing
-
Interactive conversations via
ClaudeSDKClient- Bidirectional communication
- Session management
- Interrupt support
Tests are organized by component:
TypesTests.cs- Type system testsExceptionsTests.cs- Exception handling testsMessageParserTests.cs- Message parsing testsClientTests.cs- Client functionality tests
Run tests with:
dotnet test --verbosity normalThe examples project demonstrates:
- Basic query usage
- Advanced options
- Tool integration
- Interactive conversations
- Streaming scenarios
Run examples with:
dotnet run --project examples/The SDK uses a hierarchy of custom exceptions:
ClaudeSDKException (base)
├── CLIConnectionException
│ └── CLINotFoundException
├── ProcessException
├── CLIJsonDecodeException
└── MessageParseException
- Use
IAsyncEnumerablefor streaming to avoid buffering all messages - Implement proper disposal patterns for resources
- Cancel long-running operations with
CancellationToken - Pool JSON serializer options to reduce allocations
- Follow the existing code style and patterns
- Add tests for new functionality
- Update documentation for public API changes
- Ensure compatibility with .NET 8.0+