# CommandHandle

> **Class** in `langsmith`

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

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

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)

The auto-reconnect is transparent -- the iterator reconnects and
continues yielding chunks without any user intervention. If all
reconnect attempts are exhausted, SandboxConnectionError is raised.

Construction modes (controlled by ``command_id``):
- **New execution** (``command_id=""``, the default): the constructor
  eagerly reads the server's ``"started"`` message to populate
  ``command_id`` and ``pid`` before returning.
- **Reconnection** (``command_id`` set): skips the started-message
  read, since reconnect streams don't emit one.

## Signature

```python
CommandHandle(
    self,
    message_stream: Iterator[dict],
    control: Optional[_WSStreamControl],
    sandbox: Sandbox,
    *,
    command_id: str = '',
    stdout_offset: int = 0,
    stderr_offset: int = 0,
)
```

## Description

**Example:**

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

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

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

## Constructors

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

| Name | Type |
|------|------|
| `message_stream` | `Iterator[dict]` |
| `control` | `Optional[_WSStreamControl]` |
| `sandbox` | `Sandbox` |
| `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/CommandHandle/kill)
- [`send_input()`](https://reference.langchain.com/python/langsmith/sandbox/_models/CommandHandle/send_input)
- [`reconnect()`](https://reference.langchain.com/python/langsmith/sandbox/_models/CommandHandle/reconnect)

---

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