> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Interpreter

> Run stateful notebook-style code in a hosted machine.

The code interpreter is the stateful execution layer for Python and other
notebook-style kernels. Use `template="code-interpreter"` for the standard
Python environment.

## Quick start

```python theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
from nullspace import Machine

with Machine.create(template="code-interpreter") as machine:
    execution = machine.run_code("x = 40 + 2\nx")
    print(execution.results[0].text)
```

## Contexts

```python theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
ctx = machine.code_interpreter.create_code_context(
    cwd="/workspace",
    language="python",
)
machine.code_interpreter.run_code("value = 10", context=ctx)
result = machine.code_interpreter.run_code("value * 2", context=ctx)
print(result.results[0].text)
```

## Stream callbacks

```python theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
def on_stdout(message):
    print(message.line, end="")

machine.run_code("print('streamed')", on_stdout=on_stdout)
```

Use `machine.code_interpreter.run_code()` directly when you need the full
callback surface:

```python theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
machine.code_interpreter.run_code(
    "open('/workspace/out.txt', 'w').write('done')",
    language="python",
    envs={"RUN_ID": "docs"},
    timeout=30,
    on_artifact=lambda artifact: print(artifact.id, artifact.kind),
)
```

`list_runs` is available for read-only hosted beta inspection. Queued async run
mutation/wait helpers (`create_run`, `get_run`, `cancel_run`, and
`wait_for_run`) are not part of the hosted private beta launch surface because
remote host-agent routing for queued code runs is not implemented yet. Artifact
helpers (`list_artifacts`, `get_artifact`,
`download_artifact`, `preview_artifact`, and `delete_artifact`) are supported
for hosted beta `run_code` executions.

Concept: [Exec](../../concepts/exec). API reference:
[executeCode](../../api-reference), [createCodeContext](../../api-reference),
[createCodeRun](../../api-reference), and [listCodeArtifacts](../../api-reference).
