# AsyncCommandHandle

> **Class** in `langsmith`

📖 [View in docs](https://reference.langchain.com/python/langsmith/sandbox/_models/AsyncCommandHandle)

Async handle to a running command with streaming output and auto-reconnect.

Async iterable, yielding OutputChunk objects (stdout and stderr interleaved
in arrival order). Access .result after iteration to get the full
ExecutionResult.

Auto-reconnect behavior:
- Server hot-reload (1001 Going Away): reconnect immediately
- Network error / unexpected close:    reconnect with exponential backoff
- User called kill():                  do NOT reconnect (propagate error)

Construction modes (controlled by ``command_id``):
- **New execution** (``command_id=""``, the default): call
  ``await handle._ensure_started()`` after construction to read the
  server's ``"started"`` message and populate ``command_id`` / ``pid``.
- **Reconnection** (``command_id`` set): skips the started-message
  read, since reconnect streams don't emit one.

## Signature

```python
AsyncCommandHandle(
    self,
    message_stream: AsyncIterator[dict],
    control: Optional[_AsyncWSStreamControl],
    sandbox: AsyncSandbox,
    *,
    command_id: str = '',
    stdout_offset: int = 0,
    stderr_offset: int = 0,
)
```

## Description

**Example:**

handle = await sandbox.run("make build", timeout=600, wait=False)

async for chunk in handle:    # auto-reconnects on transient errors
    print(chunk.data, end="")

result = await handle.result
print(f"Exit code: {result.exit_code}")

## Constructors

```python
__init__(
    self,
    message_stream: AsyncIterator[dict],
    control: Optional[_AsyncWSStreamControl],
    sandbox: AsyncSandbox,
    *,
    command_id: str = '',
    stdout_offset: int = 0,
    stderr_offset: int = 0,
) -> None
```

| Name | Type |
|------|------|
| `message_stream` | `AsyncIterator[dict]` |
| `control` | `Optional[_AsyncWSStreamControl]` |
| `sandbox` | `AsyncSandbox` |
| `command_id` | `str` |
| `stdout_offset` | `int` |
| `stderr_offset` | `int` |


## Properties

- `MAX_AUTO_RECONNECTS`
- `command_id`
- `pid`
- `result`
- `last_stdout_offset`
- `last_stderr_offset`

## Methods

- [`kill()`](https://reference.langchain.com/python/langsmith/sandbox/_models/AsyncCommandHandle/kill)
- [`send_input()`](https://reference.langchain.com/python/langsmith/sandbox/_models/AsyncCommandHandle/send_input)
- [`reconnect()`](https://reference.langchain.com/python/langsmith/sandbox/_models/AsyncCommandHandle/reconnect)

---

[View source on GitHub](https://github.com/langchain-ai/langsmith-sdk/blob/ce9e9e8973442b33e98ec3ce1b9c2dd3f58a43a7/python/langsmith/sandbox/_models.py#L839)