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

# Examples

> End-to-end examples with the Hexel Python SDK.

Copy-paste examples that combine multiple parts of the platform.

## Deploy an agent and manage its instance

Deploy via CLI, then use the SDK to monitor and manage:

```bash theme={"dark"}
# Deploy the instance
hexel compute instance deploy YOUR_AGENT_ID --env-file .env
```

```python theme={"dark"}
from hexel import Hexel

client = Hexel(api_key="YOUR_API_KEY")

# List instances for the agent
instances = client.compute.instance.list(agent_id="YOUR_AGENT_ID")
for inst in instances:
    print(f"{inst['id']} — {inst['state']} — {inst['endpoint']}")

# Redeploy after a config change
client.compute.instance.redeploy(instances[0]["id"])

# Stop when done
client.compute.instance.stop(instances[0]["id"])
```

## Run a data-processing job in a sandbox

```python theme={"dark"}
from hexel import Hexel

client = Hexel(api_key="YOUR_API_KEY")

sandbox = client.compute.sandbox.create(tier="large")
try:
    result = client.compute.sandbox.execute(
        sandbox["sandbox_id"],
        code="""
import json
data = [{"id": i, "value": i * 2} for i in range(100)]
print(json.dumps({"count": len(data), "total": sum(d["value"] for d in data)}))
""",
        language="python",
    )
    print(result)
finally:
    client.compute.sandbox.release(sandbox["sandbox_id"])
```

## Submit a task and handle plan approval

```python theme={"dark"}
from hexel import Hexel

client = Hexel(api_key="YOUR_API_KEY")

task = client.orchestrator.task.create(
    fleet_id="YOUR_FLEET_ID",
    environment_id="YOUR_ENVIRONMENT_ID",
    workspace_id="YOUR_WORKSPACE_ID",
    input="Draft and send the weekly status email",
)

for event in client.orchestrator.task.stream(task["id"]):
    if event["type"] == "plan.review":
        approvals = client.orchestrator.approval.list()
        for a in approvals:
            client.orchestrator.approval.review(a["id"], decision="approve")
    elif event["type"] in ("task.complete", "task.failed"):
        print("Done:", event["type"])
        break
```

## Execute a tool with preflight validation

```python theme={"dark"}
from hexel import Hexel

client = Hexel(api_key="YOUR_API_KEY")

# Validate inputs first
client.tools.preflight(
    tool_slug="GITHUB_CREATE_ISSUE",
    input={"repo": "your-org/repo", "title": "Investigate latency spike"},
)

# Execute
call = client.tools.execute(
    tool_slug="GITHUB_CREATE_ISSUE",
    input={"repo": "your-org/repo", "title": "Investigate latency spike"},
)
print(call)
```

## Batch tool execution

```python theme={"dark"}
from hexel import Hexel

client = Hexel(api_key="YOUR_API_KEY")

results = client.tools.batch([
    {"tool_slug": "SLACK_POST_MESSAGE", "input": {"channel": "#alerts", "text": "Deploy started"}},
    {"tool_slug": "SLACK_POST_MESSAGE", "input": {"channel": "#alerts", "text": "Deploy finished"}},
])
print(results)
```

## Instrument LLM calls

```python theme={"dark"}
from hexel.instrument import init, start_request, finish_request

init()  # Once at startup

start_request()
# ... your OpenAI / Anthropic calls happen here ...
telemetry = finish_request()
print(telemetry)
```

## Related pages

<CardGroup cols={2}>
  <Card title="Resources" icon="cubes" href="/docs/sdks/python/resources">
    The full client surface.
  </Card>

  <Card title="Error Handling" icon="triangle-exclamation" href="/docs/sdks/python/error-handling">
    Retries and errors.
  </Card>

  <Card title="Orchestration" icon="sitemap" href="/docs/orchestration/overview">
    Tasks, fleets, and approvals.
  </Card>

  <Card title="Tool Gateway" icon="plug" href="/docs/tool-gateway/overview">
    Connect and execute tools.
  </Card>
</CardGroup>
