A full-featured command-line application demonstrating real integration with Claude Code using the .NET SDK.
- .NET 8.0+ installed
- Claude Code CLI installed:
npm install -g @anthropic-ai/claude-code
- Anthropic API Key configured (Claude Code will prompt if not set)
This demo application showcases 4 different modes of interaction with Claude:
One-shot query with detailed response metrics.
dotnet run
# or
dotnet run simpleFeatures:
- ✅ Single question → Single answer
- ✅ Shows thinking process
- ✅ Displays timing metrics (API time, local time)
- ✅ Shows cost and turn count
- ✅ Color-coded output
- ✅ Error handling with helpful messages
Example:
Enter your question: What is the capital of France?
🤖 Claude is thinking...
Paris is the capital of France.
✓ Completed in 1234ms (API: 1100ms, Local: 1250ms)
💰 Cost: $0.0012
🔄 Turns: 1
Multi-turn conversation with context preservation.
dotnet run interactiveFeatures:
- ✅ Continuous conversation loop
- ✅ Context maintained across turns
- ✅ Type
/exitto quit - ✅ Real-time response streaming
- ✅ Error handling per response
Example:
You: What is 2 + 2?
Claude: 2 + 2 equals 4.
You: What about 3 times that?
Claude: 3 times 4 equals 12.
You: /exit
Goodbye!
Shows partial responses as they arrive.
dotnet run streamingFeatures:
- ✅ Real-time streaming with
IncludePartialMessages - ✅ Visual progress indicator (dots)
- ✅ Immediate response display
- ✅ Stream completion notification
Example:
Enter your question: Write a haiku about coding
🤖 Claude is responding (streaming)...
.......
Lines of code unfold,
Logic weaves through silent night,
Bugs in morning light.
✓ Stream completed - 1 turns
Demonstrates file operations and tool usage.
dotnet run toolsFeatures:
- ✅ File operations (Read, Write, Edit, Bash)
- ✅ Tool use visualization
- ✅ Input/output display
- ✅ Permission mode handling
- ✅ Safety warning
- ✅ Tool use count tracking
Example:
This demo allows Claude to use file tools.
⚠️ Claude can read/write files in the current directory!
Enter your request: Create a file called test.txt with "Hello World"
🔧 Claude is working with tools enabled...
I'll create that file for you.
🔧 Using tool: Write
Input: {"file_path":"test.txt","content":"Hello World"}
Result: File written successfully
✓ Completed with 1 tool uses
⏱️ Duration: 2345ms
💰 Cost: $0.0023
# Navigate to the example directory
cd examples/ClaudeChat
# Run in simple mode (default)
dotnet run
# Run in interactive mode
dotnet run interactive
# Run with streaming
dotnet run streaming
# Run with tools enabled
dotnet run tools# Build
dotnet build
# Run the built executable
dotnet run --no-build
# Create a release build
dotnet build -c Release
./bin/Release/net8.0/ClaudeChat# Publish as self-contained application (includes .NET runtime)
dotnet publish -c Release -r win-x64 --self-contained
# Publish as framework-dependent (smaller, requires .NET installed)
dotnet publish -c Release -r win-x64 --self-contained falseProgram.cs
├── Main() # Entry point, mode selection
├── RunSimpleMode() # One-shot query with metrics
├── RunInteractiveMode() # Multi-turn conversation
├── RunStreamingMode() # Streaming responses
└── RunToolsDemo() # File operations demo
-
ClaudeAgent.QueryAsync() - Simple query API
await foreach (var message in ClaudeAgent.QueryAsync(question)) { // Process messages }
-
ClaudeSdkClient - Interactive client
await using var client = new ClaudeSdkClient(options); await client.ConnectAsync(); await client.QueryAsync(input); await foreach (var msg in client.ReceiveResponseAsync()) { // Process response }
-
ClaudeAgentOptions - Configuration
var options = new ClaudeAgentOptions { AllowedTools = new List<string> { "Read", "Write" }, PermissionMode = PermissionMode.Default, MaxTurns = 50, IncludePartialMessages = true, SystemPrompt = "Custom instructions..." };
-
Message Types - Typed message handling
AssistantMessage- Claude's responsesUserMessage- User inputsResultMessage- Query results with metricsStreamEvent- Partial streaming updates
-
Content Blocks - Different content types
TextBlock- Text responsesThinkingBlock- Internal reasoningToolUseBlock- Tool invocationsToolResultBlock- Tool outputs
- ✅ Color-coded output using
Console.ForegroundColor - ✅ Unicode box drawing for header
- ✅ Progress indicators (dots, spinners)
- ✅ Error handling with helpful messages
- ✅ Metrics display (time, cost, turns)
- ✅ Tool visualization with inputs/outputs
The application handles common errors gracefully:
❌ Error: Claude Code CLI not found!
Please install Claude Code:
npm install -g @anthropic-ai/claude-code
Claude Code CLI will prompt you to configure your API key on first use.
❌ Error: Failed to start Claude Code: [error details]
With PermissionMode.Default, Claude Code will prompt for approval before dangerous operations.
Add a new case to the switch statement in Main():
case "custom":
await RunCustomMode();
break;Then implement your custom mode:
static async Task RunCustomMode()
{
var options = new ClaudeAgentOptions
{
// Your custom options
};
await foreach (var message in ClaudeAgent.QueryAsync("prompt", options))
{
// Your custom handling
}
}Modify the Console.ForegroundColor settings:
Console.ForegroundColor = ConsoleColor.Magenta; // Change to your preference
Console.WriteLine("Your text");
Console.ResetColor();Extend the AllowedTools list:
AllowedTools = new List<string>
{
"Read",
"Write",
"Edit",
"Bash",
"Glob", // File search
"Grep", // Content search
"WebFetch", // Web scraping
// See Claude Code docs for full list
}Install or reinstall Claude Code:
npm install -g @anthropic-ai/claude-codeRun with appropriate permissions, or use PermissionMode.AcceptEdits in your options (use with caution).
Set MaxTurns to limit conversation length:
MaxTurns = 5 // Limit to 5 back-and-forth exchangesThis is normal for complex queries. The application shows timing metrics to help you understand performance.
var options = new ClaudeAgentOptions
{
Cwd = "/path/to/project"
};var options = new ClaudeAgentOptions
{
Env = new Dictionary<string, string>
{
["MY_VAR"] = "value"
}
};await client.QueryAsync("Hello", sessionId: "my-session");- Use Simple Mode for one-off queries
- Use Interactive Mode for conversations
- Enable Streaming for faster perceived response time
- Limit Tools to only what you need
- Set MaxTurns to prevent runaway costs
To add new features or modes:
- Implement your mode function
- Add it to the switch statement in
Main() - Update this README with usage instructions
- Test thoroughly with the real Claude Code CLI
MIT - Same as parent project