Skip to main content
Code contexts let you preserve variables, imports, and state across multiple run_code calls.
from gravixlayer import GravixLayer

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

# Create a context
ctx = client.runtime.create_code_context(runtime.runtime_id)
print(ctx.context_id)
print(ctx.language)  # python
print(ctx.cwd)       # /workspace

# Run code in the context — variables persist
client.runtime.run_code(runtime.runtime_id, code="counter = 1", context_id=ctx.context_id)
result = client.runtime.run_code(runtime.runtime_id, code="counter += 1\nprint(counter)", context_id=ctx.context_id)
print(result.text)  # 2

client.runtime.kill(runtime.runtime_id)

Parameters

ParameterTypeRequiredDescription
runtime_idstringYesRuntime identifier
languagestringNoContext language. Default: python
cwdstringNoWorking directory. Default: /workspace

Response

FieldTypeDescription
context_idstringContext identifier (pass to run_code)
languagestringLanguage of the context
cwdstringWorking directory