Python Decorator
What you'll learn: How to use @kv.step to instrument custom Python agents, including task lifecycle, async support, and all available options.
Basic usage
Wrap each function with @kv.step(step_type, verb). The decorator handles the evaluate-execute-record cycle automatically.
from kyvvu import Kyvvu, StepType, Verb
kv = Kyvvu(api_url="https://platform.kyvvu.com", api_key="KvKey-...", agent_key="my-bot")
kv.register_agent(name="My Bot", purpose="Customer support triage and response")
@kv.step(StepType.step_model, Verb.POST)
def chat(prompt: str) -> str:
return llm.complete(prompt)
@kv.step(StepType.step_resource, Verb.GET)
def read_ticket(ticket_id: str) -> dict:
return db.get_ticket(ticket_id)When chat("Hello") is called:
The decorator constructs a
Behaviorwithstep_type=step.model,verb=POST,step_name="chat".It calls
evaluate()— the engine checks policies against the task history.If allowed, the function executes.
The completed step (with output) is recorded into history via
record().If blocked,
KyvvuBlockedErroris raised and the function does not execute.
Task lifecycle
Use StepType.task_start and StepType.task_end to mark task boundaries:
Or use the programmatic API:
All three lifecycle methods (start_task, end_task, error_task) accept optional context=, properties=, and meta= kwargs for template matching and caller overrides.
Decorator arguments
step_type
StepType
required
The atomic behaviour type (StepType.step_model, StepType.step_resource, etc.).
verb
Verb
None
HTTP-style verb (Verb.GET, Verb.POST, etc.).
step_name
str
function name
Human-readable step name. Defaults to the decorated function's __name__.
properties
dict
{}
Structured metadata merged into the Behavior's properties.
meta
dict
None
Framework-specific metadata (parent task ID, etc.).
capture_input
bool
True
Whether to capture function arguments as the Behavior's input.
capture_output
bool
True
Whether to capture the return value as the Behavior's output.
Properties example
Properties are deep-merged with template output. See Templates for merge semantics.
Async support
The decorator automatically detects async def functions:
Policy evaluation and recording are synchronous (sub-millisecond, in-process). Only the decorated function call is awaited. Error handling, blocked-step propagation, and task lifecycle work identically to sync functions.
Concurrent tasks in web servers
In web servers where multiple requests are handled concurrently, the SDK uses a ContextVar to track the active task_id per execution context. Each thread or asyncio task gets its own task.
Handling blocks
When a policy blocks a step, the decorator raises KyvvuBlockedError:
How the decorator uses templates
The decorator passes its arguments through the template system before constructing a Behavior. The template can:
Map
step_nameto additional properties (e.g. "if step_name starts withdb_, settarget.system = postgres").Override
step_typebased on naming conventions.Set default properties for all steps of a given type.
To use a custom template:
Next steps
LangChain / LangGraph — callback-based integration for LangChain agents
Templates — how template matching and deep-merge work
Tasks and History — task lifecycle and path-dependent evaluation
Last updated
