Sessions and cancellation
An open Model or Runtime is one inference session. Its caches survive between successful calls and are released when it closes.
| State | Owner |
|---|---|
| Messages shown to the user | Your application |
| Token history, KV caches and recurrent state | The model graph in the runtime session |
| Cache reuse or reset decisions | The RDL program |
| Temporary tensor storage and worker threads | Private backend implementation |
Keep conversation history
For the standard text processor, pass the complete message list each turn:
from redwood.runtime import Model
messages = [{"role": "system", "content": "Keep answers short."}]
with Model[object, str]("/path/to/compiled-llm") as model:
for question in ("What is a KV cache?", "When is it useful?"):
messages.append({"role": "user", "content": question})
answer = model.run({"messages": messages})
messages.append({"role": "assistant", "content": answer})
print(answer)
The instance retains numerical state. Your application builds and supplies the complete message history for each turn.
The supplied session-aware LLM programs use ReusePrefix to compare prefixes, retain reusable state and prefill the rest.
Start a new conversation
For a graph and processor exposing the standard reset input:
answer = model.run({"text": "Start fresh.", "reset": True})
This is an example within an open Model session. The example passes reset as an input to the model’s tensor contract.
Closing the instance and creating another gives fresh state regardless of the previous conversation. Use independent instances for independent cache histories.
Cancel active work
Call cancel() from another thread, or press Ctrl+C in the CLI. The CLI also handles process termination signals.
Native kernels check cancellation between bounded chunks of work. The backend stops workers before releasing their buffers. An external library call may delay the next checkpoint until that call returns.
After cancellation, close the instance and start a new session. A partially executed graph may already have updated caches.
For a Python application, keep cancellation handling around the full context:
from redwood.runtime import CancelledError, Model
try:
with Model[str, str]("/path/to/compiled-model") as model:
print(model.run("Explain inference."))
except (KeyboardInterrupt, CancelledError):
print("Request stopped.")
A custom processor runs synchronously in Python. Redwood checks cancellation between stages, but a long-running processor must arrange its own cooperative stop mechanism.
Failures and concurrency
A native execution failure or streaming callback exception invalidates the session. Close it and open a new instance. Input validation happens before native execution begins.
Calls on the same instance are serialized. Across instances, only one session can execute native inference at a time per Python process. An overlapping run from another session raises ExecutionError.
Applications decide how to queue requests, retain messages and expose a service. Those choices are outside the backend interface.