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

# Code Control

> Let an LLM control a robot by writing Python: attach CodeControl to an endpoint so the agent gets the python tool, then point a tool-calling agent at it.

This guide outlines how to let an LLM control a robot with code. `CodeControl` serves an embodiment's parts to a Python kernel and the kernel's `robot.move` follows [Code Control](https://docs.rua.hud.ai/interfaces/code-as-policy.md). It is attached the way `DirectControl` is in the [Direct Control guide](https://docs.rua.hud.ai/guides/direct-control.md), and it takes the same embodiments.

**1. Write a single-env bridge**

Code control drives single-env sims. The action needs a control kind, `names`, and bounds, as the `PointArm` bridge in [Writing a bridge](https://docs.rua.hud.ai/guides/writing-a-bridge.md#a-bridge) has.

**2. Attach CodeControl to the endpoint**

```python env.py theme={"dark"}
import math

from hud import Environment

from rua.code import CodeControl
from rua.embodiments.contract import ContractEmbodiment
from rua.sim import RobotEndpoint
from sim import CONTRACT, PointArm

env = Environment(name="arm")
sim = RobotEndpoint(PointArm).attach(env)
speeds = {"position": 0.5, "rotation": math.pi, "joint": math.pi, "grip": 2.0}
embodiment = ContractEmbodiment(CONTRACT, gripper=(0.0, 1.0), max_speed=speeds)
control = CodeControl(embodiment).attach(sim)


@env.initialize
async def up() -> None:
    await sim.start()
    for capability in await sim.capabilities():
        env.add_capability(capability)


@env.shutdown
async def down() -> None:
    await sim.stop()


@env.template()
async def reach(max_ticks: int | None = 200):
    ep = await sim.reset(max_ticks=max_ticks)
    yield {"prompt": ep["prompt"]}
    yield await sim.result(**control.result_extra())
```

The embodiment is the same one `DirectControl` takes, so a robot with its own embodiment can check more than a contract does. `sim.capabilities()` now returns `robot` and `control`, an `mcp` capability with the tools `python`, `view_image` and `done`. `control.result_extra()` adds the sandbox mode, the cells run and the robot calls to the episode's result.

**3. Point a tool-calling agent at it**

Any agent that opens `control` and calls its tools works:

```python theme={"dark"}
client = await run.client.open("control")
tools = await client.list_tools()
result = await client.call_tool(
    "python",
    {
        "code": "robot.move([{'t': 2.0, 'arm': {'x': 0.5}}])\nrobot.observe().state['arm']",
        "note": "the gripper is left of the goal",
    },
)
```

**4. Or use a language-model agent**

`create_code_agent` builds a HUD gateway agent with the Code Control prompt. It needs `HUD_API_KEY`:

```python theme={"dark"}
from rua.code import create_code_agent

agent = create_code_agent(
    "claude-sonnet-4-6", spec=embodiment.spec(), settings=control.code, max_turns=30
)
```

A Claude agent keeps the images of its last 10 turns, and older ones come back through `view_image`. For another model, pass `images_kept=None`.

Settings come from `RUA_CC_<NAME>` variables, or from the `settings` argument of `CodeControl(embodiment, settings=...)`, which wins. They are in the [reference](https://docs.rua.hud.ai/reference/code-control.md#settings).

The agent now has `python` and `view_image`, and replies as described in the [reference](https://docs.rua.hud.ai/reference/code-control.md). Every played tick also streams to the rollout's trace when `HUD_API_KEY` is set in the environment's process.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.