# Use cases and traces

> Tie every call to the use case that made it, so its cost and its speed show up where you look for them.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://ai.docs.sypnia.com/using/cases-and-traces/

For agents: Case attribution: Sypnia-Case header or SDK case option; ids from the Cases map (cases/<area>/<id>.case.yaml); unknown ids accepted and flagged (case_unknown in usage, scope error in Cases when the project is linked). Inside a traced request (@sypnia/telemetry), the SDK sends traceparent and reads the active case, so case can be omitted. Each call = span named chat <task> or embeddings <task> in that trace (gen_ai.* attributes); fallback attempts = child spans named attempt. A use case file can say it uses AI (ai: {tasks: [allergen-answer]}) and the Cases check compares with real usage. Usage views by case in App, the console and ai usage --by case.

Every call can say which use case of your map it belongs to. Then «what does AI cost us?» becomes «what does each thing people do cost us?», and a slow answer shows up in the trace of the case a person was in.

## Saying the case

```ts
await ai.chat({ task: 'allergen-answer', case: 'ask-about-a-product', messages });
```

or the header `Sypnia-Case: ask-about-a-product`. The id is the one in your map, from `cases/<area>/<id>.case.yaml` ([Cases](https://cases.docs.sypnia.com/)).

When your server is traced with Telemetry's SDK, the case of the current request is already known: `@sypnia/ai` reads it, and sends the trace along, so you can leave `case` out.

An id that is not on the map is accepted and marked `case_unknown` in usage. When the project is linked to Cases, it also appears there as a scope error, with the case to add.

## The use case says it uses AI

`cases/products/ask-about-a-product.case.yaml`

```yaml
id: ask-about-a-product
title: Ask about a product
actor: customer
goal: Know whether a product is safe for me before I order it
where: shop.product-detail
ai:
  tasks: [allergen-answer]
failures:
  - name: TooManyQuestions
    when: the customer reached today's limit of questions
    tell: «You asked a lot today. You can ask us at the counter, or again tomorrow.»
```

The Cases check compares that with reality: a case that calls a task it does not list, or lists one it never calls, is reported.

## In the trace

Each call is a step (a span) inside the trace of its case, next to the database queries and the page that started it:

| Span                               | What it carries                                                                                                      |
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `chat allergen-answer`             | the route, the model that answered, tokens read and written, time to the first token, total time, credits, `call_id` |
| `attempt` (one per fallback tried) | the model, why it failed                                                                                             |

The names follow OpenTelemetry's conventions for AI calls, so any OpenTelemetry tool reads them. See [Logs in Telemetry](https://ai.docs.sypnia.com/safety/logs-and-telemetry) for every attribute.
