> ## 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

> Python SDK object reference for stateful code execution, contexts, runs, and artifacts.

`machine.code_interpreter` runs notebook-style code in persistent kernel
contexts. Use it when the workload is cells, rich outputs, packages, or
artifacts rather than shell commands.

## Execution

| Method                                                                                                                                       | Use                                                                   |
| -------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| `run_code(code, language="python", context=None, envs=None, timeout=None, on_stdout=None, on_stderr=None, on_result=None, on_artifact=None)` | Execute a code cell and collect logs, results, artifacts, and errors. |
| `install_packages(packages)`                                                                                                                 | Install packages into the code interpreter environment.               |

```python theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
execution = machine.code_interpreter.run_code(
    "import pandas as pd\nprint(pd.__version__)",
    language="python",
)
print(execution.logs.stdout)
```

## Contexts

| Method                                   | Use                                 |
| ---------------------------------------- | ----------------------------------- |
| `create_code_context(language="python")` | Create a persistent kernel context. |
| `list_code_contexts()`                   | List contexts.                      |
| `restart_code_context(context)`          | Restart a kernel context.           |
| `interrupt_code_context(context)`        | Interrupt running code.             |
| `remove_code_context(context)`           | Delete a context.                   |

## Runs And Artifacts

| Method                                           | Use                        |
| ------------------------------------------------ | -------------------------- |
| `list_runs(...)`                                 | Inspect async run records. |
| `list_artifacts(run_id=None, context_id=None)`   | List produced artifacts.   |
| `get_artifact(id)`                               | Inspect artifact metadata. |
| `download_artifact(id)` / `preview_artifact(id)` | Fetch artifact bytes.      |
| `delete_artifact(id)`                            | Delete an artifact.        |

Queued run mutation helpers exist in the SDK but are not part of the hosted
private-beta launch surface until remote host-agent routing for queued code
runs is enabled.

## Related

* [Code Interpreter guide](../../code-interpreter/overview)
* [Contexts](../../code-interpreter/contexts)
* [Runs and artifacts](../../code-interpreter/runs-artifacts)
