---
title: "C#/.NET SDK"
description: "The .NET SDK wraps the Autohand Code CLI subprocess and exposes async APIs for agent runs, event streams, permissions, structured output, and cancellation-aware host code."
sidebar:
  label: "C#/.NET"
seo:
  title: "C#/.NET SDK | Autohand Code Agent SDK"
  description: "Use the C# SDK to control Autohand Code agents from .NET 8 services, developer tools, desktop apps, and automation."
---

:::note
**Repository:** [autohandai/code-agent-sdk-csharp](https://github.com/autohandai/code-agent-sdk-csharp). This SDK is in beta while the Agent SDK APIs stabilize. Pin commits or package versions in production.
:::

## Requirements

- .NET 8 or newer.
- Autohand Code CLI installed, authenticated, and configured.
- Optional `AUTOHAND_CLI_PATH` when testing a local CLI build.

## Install

```bash title=".NET"
dotnet add package Autohand.CodeAgentSdk
```

```bash title="Source"
# Until the NuGet package is published, reference the project or repository directly from your solution.
```

## Run your first agent [#first-agent]

Start with the high-level agent API when you want the SDK to own the run lifecycle and final result collection.

```csharp
using Autohand.CodeAgentSdk;

await using var agent = await Agent.CreateAsync(new AgentOptions
{
    WorkingDirectory = ".",
    Instructions = "Review code with staff-level C# judgement.",
});

var run = agent.Send("List the main source files and tell me what looks risky.");

await foreach (var item in run.StreamAsync())
{
    if (item is MessageUpdateEvent message)
    {
        Console.Write(message.Delta);
    }
}

var result = await run.WaitAsync();
Console.WriteLine(result.Text);
```

## API shape

- `Agent` and `Run` for high-level application workflows.
- `AutohandSdk` for low-level JSON-RPC control.
- `IAsyncEnumerable<SdkEvent>` for streaming tokens, tools, permission requests, and errors.
- `CancellationToken` and `await using` support for host-managed lifecycle.

## Structured JSON [#extra]

Use `RunJsonAsync<T>` when your app needs typed output instead of prose.

```csharp
using Autohand.CodeAgentSdk;

await using var agent = await Agent.CreateAsync(new AgentOptions
{
    WorkingDirectory = ".",
    Instructions = "Prefer concise release-readiness analysis.",
});

var risk = await agent.RunJsonAsync<ReleaseRisk>(
    "Assess this SDK repository for publish readiness. Do not execute commands.",
    new JsonRunOptions
    {
        SchemaName = "ReleaseRisk",
        Schema = new
        {
            summary = "string",
            risks = new[]
            {
                new { title = "string", severity = "low | medium | high" },
            },
        },
    });

Console.WriteLine(risk.Summary);

public sealed record ReleaseRisk(string Summary, Risk[] Risks);
public sealed record Risk(string Title, string Severity);
```

## Next steps

- Use [Quickstart](/agent-sdk/quickstart) for the cross-language setup flow.
- Open [C#/.NET API](/agent-sdk/csharp-api) for the reference surface.
- Read [Stream responses in real-time](/agent-sdk/io/streaming) and [Handle approvals and user input](/agent-sdk/io/approvals) for shared runtime behavior.
