---
title: "TypeScript SDK"
description: "The TypeScript wrapper is the most complete path for Node.js and Bun apps that want agent sessions, event streaming, per-step run control, concurrent-agent awareness, JSON output, and direct permission control."
sidebar:
  label: "TypeScript"
seo:
  title: "TypeScript SDK | Autohand Code Agent SDK"
  description: "TypeScript guide for the Autohand Code Agent SDK with current install commands, high-level agent APIs, streaming events, and permission handling."
---

## Install

```package-install
npm i @autohandai/agent-sdk
```

The TypeScript SDK is published on npm as [`@autohandai/agent-sdk`](https://www.npmjs.com/package/@autohandai/agent-sdk).

## Start the low-level SDK [#start-sdk]

Use `AutohandSDK` when you want to manage the CLI lifecycle yourself.

```ts
import { AutohandSDK } from '@autohandai/agent-sdk';

async function main() {
  const sdk = new AutohandSDK({
    cwd: '.',
    debug: true,
  });

  await sdk.start();

  for await (const event of sdk.streamPrompt({
    message: 'Explain what src/index.ts is responsible for.'
  })) {
    if (event.type === 'message_update') {
      process.stdout.write(event.delta);
    }
  }

  await sdk.stop();
}

main();
```

## Use the high-level agent API [#agent-api]

Use `Agent.create()` when you want a longer-lived session with cleaner application code.

```ts
import { Agent } from '@autohandai/agent-sdk';

const agent = await Agent.create({
  cwd: '.',
  instructions: 'Prefer Bun commands and focused edits.',
  permissionMode: 'interactive',
});

const run = await agent.send('Review the repository for release risks.');

for await (const event of run.stream()) {
  if (event.type === 'message_update') {
    process.stdout.write(event.delta);
  }
}

const result = await run.wait();
console.log(result.text);
await agent.close();
```

## Pause at completed tool steps [#step-control]

Pass `stopWhen` to `agent.run()` or `agent.send()` when your application needs to inspect a completed tool step before the agent continues.

**What's new:** updated builds keep cancellation responsive during async conditions and expose typed rate-limit events. Read the [TypeScript development update](/agent-sdk/overview#whats-new) for availability, runtime requirements, and verification scope.

```ts
import {
  Agent,
  isStepCount,
} from '@autohandai/agent-sdk';

const agent = await Agent.create({ cwd: '.' });

try {
  const first = await agent.run(
    'Inspect this repository and identify the first release risk.',
    { stopWhen: isStepCount(1) },
  );

  console.log(first.status); // 'stopped'
  console.log(first.steps);

  // Continue in the same agent session with its existing history.
  const next = await agent.run('Continue from that result.');
  console.log(next.text);
} finally {
  await agent.close();
}
```

Use `isStepCount(count)`, `hasToolCall(toolName)`, or an async custom condition. Read [Pause and resume agents with stopWhen](/agent-sdk/io/step-control) for result types, event semantics, and failure handling.

## Coordinate concurrent agents [#session-awareness]

Choose a session-awareness tier when multiple local Autohand agents may work in the same repository. The default `warn` tier reports likely Git, file-claim, and repository-drift conflicts without taking control away from the host.

```ts
const agent = await Agent.create({
  cwd: '/path/to/project',
  sessions: { awareness: 'coordinate' },
});

const peers = await agent.getSessionPeers();
console.log(peers);
```

The `passive`, `warn`, and `coordinate` tiers are local-machine coordination modes. Hosts can observe `session_peer_joined`, `session_peer_updated`, `session_peer_left`, and recoverable `session_awareness_error` events. See [Concurrent session awareness](/agent-sdk/concepts/sessions#concurrent-session-awareness).

## Handle permissions [#permissions]

Interactive mode emits `permission_request` events. Your app decides how to respond.

```ts
for await (const event of sdk.streamPrompt({
  message: 'Run the tests and report failures.'
})) {
  if (event.type === 'permission_request') {
    await sdk.permissionResponse({
      requestId: event.requestId,
      allowed: event.tool !== 'run_command',
      remember: false,
    });
    continue;
  }

  if (event.type === 'message_update') {
    process.stdout.write(event.delta);
  }
}
```

## Important event types [#events]

- `message_update`: streaming assistant text.
- `tool_start`, `tool_update`, `tool_end`: tool execution lifecycle.
- `step_end`: a completed tool-step boundary, including its calls and results.
- `permission_request`: runtime pause for approval.
- `session_peer_joined`, `session_peer_updated`, `session_peer_left`: concurrent local agent presence.
- `error`: transport, runtime, or execution failure.

## Configuration notes [#config]

The TypeScript SDK reads provider setup from the CLI config file. A typical setup looks like this:

```json
{
  "provider": "openrouter",
  "openrouter": {
    "apiKey": "sk-or-...",
    "model": "openrouter/auto"
  }
}
```

`cwd: "."` works as expected, and leaving `cwd` unset falls back to `process.cwd()`.

## Next steps

- Read [Pause and resume agents with stopWhen](/agent-sdk/io/step-control) for controlled multi-step runs.
- Read [Work with sessions](/agent-sdk/concepts/sessions#concurrent-session-awareness) for concurrent-agent coordination.
- Read [Get structured output from agents](/agent-sdk/io/structured-output) if you want typed JSON workflows.
- Read [Handle approvals and user input](/agent-sdk/io/approvals) for full approval flows.
- Open [TypeScript API](/agent-sdk/typescript-api) for the reference surface.
