# Instrument custom Python

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)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.layerlens.ai/6.-build-wire-your-code/instrument-custom-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
