> For the complete documentation index, see [llms.txt](https://docs.layerlens.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.layerlens.ai/6.-build-wire-your-code/instrument-custom-python.md).

# Instrument custom Python

Recipe — instrument a custom Python pipeline using @trace and span().

For pipelines that don't use one of the supported framework or provider adapters, use the manual instrumentation primitives from `layerlens.instrument`:

```python
from layerlens.instrument import trace, span

@trace
def run_pipeline(query: str):
 with span("retrieval"):
 chunks = retrieve(query)
 with span("llm-call"):
 answer = llm_call(query, chunks)
 return answer
```

The decorator and context manager emit spans to the configured HTTP sink. Tags, status, and error capture are handled automatically.

For full canonical patterns, see [`samples/integrations/openai_instrumented.py`](https://github.com/layerlens/stratix-python/blob/main/samples/integrations/openai_instrumented.py) — uses `@trace` and `span()` alongside `instrument_openai()`.

## See also

* [Integrations](/6.-build-wire-your-code/migration.md)
* [Concept: Traces and spans](/6.-build-wire-your-code/traces-and-spans.md)
* [Workflow: Instrument](/6.-build-wire-your-code/workflow.md)
