@tool; instead, they are accessed from inside PyRepl.execute_code(...) through the runtime.* namespace.
What They Are For
In CodeAct mode, the model writes Python code and executes it throughexecute_code. Runtime primitives are the runtime APIs available from that code, for example:
Runtime Entry Point
Access capabilities directly from
runtime.* without imports.CodeAct Built-in Capability
Hosted by
PyRepl and reached indirectly through execute_code.Discoverable by the Model
Exposed through
runtime.get_primitive_spec(...) and runtime.list_primitive_specs(...).Key Concepts
PrimitivePack
PrimitivePack
A
PrimitivePack is a namespace for runtime primitives.- It defines the
runtime.<pack_name>.<primitive_name>prefix - It can bind a default backend
- It can carry
guidancethat explains the mental model and boundaries of the pack
Primitive
Primitive
A primitive is an individual runtime function under a pack.It is implemented as a regular Python function that also receives a
PrimitiveCallContext.Backend
Backend
A backend is the Python object that provides the real capability, such as a service client, state container, or custom class instance. Primitive handlers usually access it through
ctx.backend or ctx.get_backend(...).Recommended Development Flow
Create a pack
Use
repl.pack(name, backend=..., guidance="...") to create a namespace and backend binding.@repl.primitive(...) directly.
Where They Are Used
PyRepl.execute_code
PyRepl.execute_code
Exposes the
runtime.* namespace inside the execution environment.llm_chat(toolkit=repl.toolset)
llm_chat(toolkit=repl.toolset)
The model reaches primitives only after deciding to call
execute_code.Built-in selfref pack
Built-in selfref pack
runtime.selfref.context.* and runtime.selfref.fork.* are built-in examples of the same mechanism.Primitive Context Injection
Each primitive handler receives aPrimitiveCallContext, which typically includes:
primitive_namecall_id/execution_idevent_emitterrepl/registrybackend_namebackend
Prefer accessing runtime dependencies through
ctx.backend or ctx.get_backend(...) instead of looking them up manually. That keeps fork, clone, and lifecycle behavior predictable.Contracts and Discoverability
How the model discovers primitives
How the model discovers primitives
Use:
runtime.get_primitive_spec(name)for a single primitiveruntime.list_primitive_specs(...)for batches of primitives
format='dict' can be used for structured fields.Where contract data comes from
Where contract data comes from
- The handler docstring
PrimitiveContract- Explicit arguments on
@primitive(...)
PrimitiveContract vs PrimitiveSpec
PrimitiveContract vs PrimitiveSpec
PrimitiveContract: the author-side structured definitionPrimitiveSpec: the normalized runtime/public specification actually consumed by the model
Recommended docstring fields
The docstring format commonly includes:Use:Input:Output:Parse:Parameters:Best Practices:
Best Practices must come from the docstring and are required for registration.How Primitives Enter Prompt Context
Primitives themselves are not injected directly into the system prompt. What usually gets injected is tool guidance, especially fromexecute_code.
In practice, primitive guidance reaches the model through two paths:
execute_codebest practices that tell the model how to inspect primitive contracts- runtime queries such as
runtime.get_primitive_spec(...)andruntime.selfref.guide()
prompt_injection_builder.