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

# AI Integration

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

Gravix Layer lets your LLM application run real code in isolated Runtimes.

## Recommended Integration Loop

1. Receive user request.
2. Create or reuse a runtime.
3. Execute generated code or terminal commands.
4. Read outputs/files.
5. Return a summarized answer.
6. Clean up runtime resources.

## Example

<CodeGroup>
  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from gravixlayer import GravixLayer


  def run_python_task(user_prompt: str) -> str:
      client = GravixLayer()  # defaults to cloud="aws", region="us-east-1"
      sandbox = None

      try:
          sandbox = client.runtime.create()  # defaults to template="base-small"

          code = f"""
  prompt = {user_prompt!r}
  print('Received prompt:', prompt)
  print('Computation complete')
  """
          response = sandbox.run_code(code=code)

          if response.success:
              return response.text
          return f"Execution error: {response.error.value if response.error else 'unknown'}"

      finally:
          if sandbox is not None:
              sandbox.kill()
  ```

  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import { GravixLayer } from 'gravixlayer';

  async function runPythonTask(userPrompt: string): Promise<string> {
    const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
    const sandbox = await client.runtime.create(); // defaults to template="base-small"

    try {
      const code = `
  prompt = ${JSON.stringify(userPrompt)}
  print('Received prompt:', prompt)
  print('Computation complete')
  `;
      const response = await sandbox.runCode(code);
      if (response.success) return response.text;
      const err = response.error;
      const detail = typeof err === 'string' ? err : err?.value ?? 'unknown';
      return `Execution error: ${detail}`;
    } finally {
      await sandbox.kill();
    }
  }
  ```

  ```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # Requires: GRAVIXLAYER_API_KEY, jq
  # CLI has no inline run_code — write generated code to a temp file.

  RT=$(gravixlayer runtime create --wait --output json | jq -r '.runtime_id')

  cat > /tmp/task.py <<'EOF'
  prompt = "summarize sales"
  print("Received prompt:", prompt)
  print("Computation complete")
  EOF

  gravixlayer runtime run "$RT" /tmp/task.py
  gravixlayer runtime kill "$RT" -y
  ```

  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # Requires: GRAVIXLAYER_API_KEY, jq
  API="https://api.gravixlayer.ai/v1"
  AUTH=(-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" -H "Content-Type: application/json")

  RT=$(curl -sS -X POST "$API/agents/runtime" "${AUTH[@]}" \
    -d '{"template":"base-small","cloud":"aws","region":"us-east-1"}' | jq -r '.runtime_id')

  for i in $(seq 1 60); do
    STATUS=$(curl -sS "$API/agents/runtime/$RT" -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" | jq -r '.status')
    [ "$STATUS" = "running" ] && break
    sleep 2
  done

  curl -sS -X POST "$API/agents/runtime/$RT/code/run" "${AUTH[@]}" \
    -d '{"code":"prompt = \"summarize sales\"\nprint(\"Received prompt:\", prompt)\nprint(\"Computation complete\")","language":"python"}'
  echo

  curl -sS -X DELETE "$API/agents/runtime/$RT" -H "Authorization: Bearer $GRAVIXLAYER_API_KEY"
  ```
</CodeGroup>

## Production Guidance

* Always use cleanup in `finally` blocks.
* Keep API keys in environment variables.
* Use SSH only when an operator needs direct shell access.

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

  <Card title="Terminal Execution" icon="terminal" href="/documentation/agentruntime/command-execution/overview">
    Run shell commands and CLI tooling
  </Card>
</CardGroup>
