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

# LLM Integration

> Connect LLM applications to Gravix Layer for code execution, terminal workflows, and file operations

Gravix Layer provides isolated agent runtimes that your LLM application can use to run generated code, execute commands, and manage files.

## Integration Pattern

1. Receive a user request.
2. Create or reuse an agent runtime.
3. Execute LLM-generated code or terminal commands.
4. Read outputs and files.
5. Return results to the user.
6. Clean up runtime resources.

## Example

```python theme={null}
from gravixlayer import GravixLayer


def run_task(code: str) -> str:
    client = GravixLayer()
    runtime = None

    try:
        runtime = client.runtime.create(template="python-3.14-base-small")

        result = runtime.run_code(code=code)

        if result.success:
            return result.text
        return f"Error: {result.error.value if result.error else 'unknown'}"

    finally:
        if runtime:
            runtime.kill()
```

## Best Practices

* Store API keys in environment variables, never in code.
* Always clean up runtimes in `finally` blocks.
* Use code contexts to persist state across multiple `run_code` calls.
* Use `run_cmd` for package installs and shell operations.
* Set appropriate timeouts for long-running workloads.

<CardGroup cols={2}>
  <Card title="Code Execution" icon="code" href="/documentation/agentruntime/code-execution/run-python">
    Run Python and JavaScript
  </Card>

  <Card title="Terminal" icon="terminal" href="/documentation/agentruntime/command-execution/run-command">
    Shell commands and scripts
  </Card>
</CardGroup>
