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

# Code Execution Overview

> Execute Python and JavaScript in isolated Runtimes

Use `run_code` for language-level execution with structured outputs, logs, and error objects.

## Supported Languages

* Python
* JavaScript

## Basic Example

`POST /v1/agents/runtime/{runtime_id}/code/run`

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

  client = GravixLayer()  # defaults to cloud="aws", region="us-east-1"
  sandbox = client.runtime.create()  # defaults to template="base-small"

  response = sandbox.run_code(code="print('Hello from code executor')")
  print(response.text)

  sandbox.kill()
  ```

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

  const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
  const sandbox = await client.runtime.create(); // defaults to template="base-small"

  const response = await sandbox.runCode('print(\'Hello from code executor\')');
  console.log(response.text);

  await sandbox.kill();
  ```

  ```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark"}}
  cat > /tmp/hello.py <<'EOF'
  print("Hello from code executor")
  EOF

  gravixlayer runtime run "$RT" /tmp/hello.py --timeout 300
  ```

  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/run" \
    -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "code": "print('\''Hello from code executor'\'')",
      "language": "python",
      "timeout": 300
    }' | jq .
  ```
</CodeGroup>

## Why Use Code Contexts

Code contexts let you preserve state between calls. Create a context, then pass `context_id` on each `run_code` call (Python/cURL). The CLI can create contexts with `gravixlayer runtime code-context create` but `runtime run` does not accept `--context-id`.

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

  client = GravixLayer()  # defaults to cloud="aws", region="us-east-1"
  sandbox = client.runtime.create()  # defaults to template="base-small"

  ctx = client.runtime.create_context(sandbox.runtime_id)
  client.runtime.run_code(sandbox.runtime_id, code="x = 10", context_id=ctx.context_id)
  print(client.runtime.run_code(sandbox.runtime_id, code="print(x + 1)", context_id=ctx.context_id).text)

  sandbox.kill()
  ```

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

  const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
  const sandbox = await client.runtime.create(); // defaults to template="base-small"

  const ctx = await sandbox.createContext();
  await sandbox.runCode('x = 10', { contextId: ctx.contextId });
  console.log((await sandbox.runCode('print(x + 1)', { contextId: ctx.contextId })).text);

  await sandbox.kill();
  ```
</CodeGroup>

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

  <Card title="Run JavaScript" icon="js" href="/documentation/agentruntime/code-execution/run-javascript">
    JavaScript execution examples
  </Card>

  <Card title="Create Context" icon="layers" href="/documentation/agentruntime/code-execution/create-context">
    Persist variables and imports across calls
  </Card>
</CardGroup>
