Python decorators
The supported launch path uses two decorators. Both work with regular and async def functions.
Install and initialize
pip install staso
export STASO_API_KEY=ak_...import staso as st
st.init(agent_name="support-agent")agent_name and the API key are required. You can pass the key directly as api_key="ak_...".
@st.agent
Put @st.agent on the entry point for one agent run. It creates the root span and gives nested provider and tool spans the same trace ID.
@st.agent
def research(query: str) -> str:
return answer(query)
@st.agent(name="billing-agent", tags=["billing"])
async def bill(account_id: str) -> str:
return await process(account_id)@st.tool
Put @st.tool on the Python function that performs tool work. The decorator records the actual duration, status, arguments, return value, and propagated exception.
@st.tool
def search_docs(query: str) -> list[str]:
return index.search(query)
@st.tool(name="publish_report")
async def publish_report(report_id: str) -> bool:
return await reports.publish(report_id)@st.tool observes execution. It does not run Guard by itself. Provider-proposed tools are evaluated in supported patched provider loops; direct calls need an explicit st.guard() check when they require enforcement.
Provider patches retain proposal IDs to link a later tool-result continuation. They do not infer execution duration, success, output, or errors from the gap between model calls.
Guard direct tool calls explicitly
Check a direct call before running it when the action needs enforcement:
@st.tool
def delete_record(record_id: str) -> None:
db.records.delete(record_id)
@st.agent
def remove(record_id: str) -> str:
decision = st.guard("delete_record", {"record_id": record_id})
if decision.action == "block":
return f"Blocked: {decision.reason}"
delete_record(record_id)
return "Deleted"Patched OpenAI and Anthropic integrations evaluate provider-proposed tool calls before the normal dispatch loop receives them. See the OpenAI and Anthropic quickstarts.
Parameters
| Parameter | Default | Description |
|---|---|---|
name | Function name | Span and tool label. |
tags | None | String tags stored in span metadata. |
metadata | None | Additional serializable span metadata. |
capture_input | True | Record function arguments. |
capture_output | True | Record the return value. |
Both bare and configured forms work:
@st.agent
def run() -> None: ...
@st.tool(name="lookup", capture_output=False)
def lookup(account_id: str) -> dict: ...Errors
When the wrapped function raises or an async task is cancelled, Staso marks the span as an error, records available exception details, and propagates the original exception or cancellation.
Capture controls
Set capture_input=False or capture_output=False to omit that decorator payload. This is capture suppression, not content redaction.
The local flag is combined with the global category policy:
| Decorator payload | Required global category |
|---|---|
@st.agent or @st.trace arguments | request |
@st.tool arguments | tool_arguments |
| Any decorated return value | output |
| Caller-provided decorator metadata | request |
A local False always wins. When effective input capture is off, the wrapper does not materialize function arguments for telemetry. Error prose is omitted whenever the effective content policy is not fully enabled; the exception class and error status remain operational.
Confirm delivery
Short-lived processes should shut down after the final run and inspect the structured result:
report = st.shutdown(timeout=5)
if not report.ok:
raise RuntimeError(f"Trace delivery incomplete: {report.last_failure}")Use st.flush(timeout=5) instead in a warm serverless worker. It waits for events accepted before the call and keeps the client ready for the next invocation.
DeliveryReport.ok is false for delivery failures, pending events, timeouts, or local rejections. rejected counts spans the local queue could not accept, including spans ended automatically by decorators. last_rejection provides the safe failure category.