Skip to main content
GravixLayer 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

from gravixlayer import GravixLayer


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

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

        result = client.runtime.run_code(runtime.runtime_id, code=code)

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

    finally:
        if runtime:
            client.runtime.kill(runtime.runtime_id)

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.

Code Execution

Run Python and JavaScript in isolated runtimes

Command Execution

Run shell commands and CLI tooling