# Metadata, users and threads

> Tag a run with your own metadata, the end user it acts for and the conversation it belongs to, so Pyrula Cloud can filter by them.

Source: https://docs.pyrula.io/workflows/guides/metadata-and-users/

A submit carries three things besides the run's input: `metadata` (your own key-value labels),
`user_id` (the end user the run acts for) and `thread_id` (the conversation it belongs to). Pyrula
records them on the run's `run:init` event and Pyrula Cloud filters runs by each of them.

The following example submits a support workflow for one customer, inside one conversation:

```python
import asyncio

from pyrula.workflows import InlineWorkflowRunner, MemoryStore, workflow


@workflow
async def refund(ctx, order_id: str) -> str:
    # The same user on every attempt, retry and saved-case or what-if run of it.
    return f"refunded {order_id} for {ctx.user_id}"


async def main() -> None:
    runner = InlineWorkflowRunner(store=MemoryStore(), cloud=False)
    handle = await runner.client.submit(
        "refund",
        {"order_id": "A-1042"},
        user_id="cus_8f2a",
        thread_id="support-7731",
        metadata={"channel": "web", "plan": "pro", "priority": 2},
    )
    await runner.run("refund", handle.run_id)
    print(await handle.result())


asyncio.run(main())
```

An agent takes the same three arguments beside its parameters:

```python
turn_id = runtime.submit(
    "support", message="where is my order?", user_id="cus_8f2a", thread_id="support-7731"
)
```

Over HTTP they are top-level fields of the submit body, beside the parameters:

```bash
curl -X POST localhost:8000/agents/support \
  -H 'content-type: application/json' \
  -d '{"message": "where is my order?", "user_id": "cus_8f2a", "metadata": {"channel": "web"}}'
```

## Metadata

Metadata is flat: string keys (at most 32, 1 to 64 characters from `[A-Za-z0-9_.-]`, not starting with `_`) and string values of at most 256 characters. A number or boolean is stored as a string (`2` becomes `"2"`). Anything else, or a value over a bound, raises `ConfigurationError` from `submit` (422 `invalid_request` over HTTP), naming the key and the limit.

A tag is a metadata key whose value is `"true"`.

## End user

`user_id` is a string of 1 to 256 characters that you choose. Send an opaque id from your own
system (a customer or account id), not an email address or a name. Pyrula never fills it in from the
authenticated caller: `owner`, the principal an HTTP router records for access checks, stays in the
runtime and never reaches Cloud.

The run's body reads it as `ctx.user_id`, and so does a step's context and a sub-agent's. Children, retries and replays inherit the user, including `continue_as_new` successors, saved cases and what-if runs. `run_and_await(..., user_id=...)` and an agent's `stream(user_id=...)` take it the same way. A Kafka binding reads it from each record: `@kafka_workflow(..., user_id=...)` and `@kafka_agent(..., user_id=...)` take a function from the decoded payload to the id, or None for no user.

## Thread

`thread_id` groups the runs of one conversation: 1 to 128 characters from `[A-Za-z0-9._-]`. On an agent, it binds the turn to that thread's history (`ctx.thread`), and over HTTP the thread must exist and belong to the caller. On a workflow it only groups the run with the conversation in Cloud and is not checked. A `continue_as_new` successor and a retry keep the thread; a child run does not, and replays never take it.

A workflow reads the user as `ctx.user_id`, and a workflow parameter named `user_id` or `thread_id` can't be submitted over HTTP, so rename it.

## What Cloud receives

`user_id` and `thread_id` ship at every capture level and `metadata` from `lite`. See [What leaves your runtime](https://docs.pyrula.io/getting-started/what-leaves-your-runtime.md).
