# Span status codes: Unset, Error and Ok

> How to mark an OpenTelemetry span as an error: status code Error plus an error.type attribute. Why success stays Unset, and when to use Ok. With code.

Source: https://hashspan.dev/learn/otel-span-status

By Selim Aytaç, maintainer · Published Oct 1, 2026 · Updated Oct 19, 2026

## Short answer

A span's status is Unset, Error or Ok. Unset is the default: nobody set a status. Instrumentation leaves successful operations Unset. Instrumentation sets Error when the operation failed, together with an error.type attribute that names the class of error. Ok is for a developer or operator who wants to mark success explicitly.

To mark a span as failed, set its status code to `Error` and add an `error.type` attribute. Leave successful spans `Unset`.

In my demo run, the agent's `withdraw_from_vault` tool span has no error. The transaction it sent reverted. Both spans are right, because they describe different operations.

Figure: Span status in one agent run: the tool span and the send span are Unset, while the confirm span of the reverted transaction is Error with error.type reverted.

## What are the three status codes?

| Code | Means | Who sets it |
|---|---|---|
| `Unset` | The default: no status was set. Instrumentation leaves success here. | Nobody, it's what you get |
| `Error` | The operation failed | Instrumentation, when it fails |
| `Ok` | Explicitly marked successful | An application developer or operator |

The semantic conventions say instrumentation must leave status Unset when an operation ends without errors ([recording errors](https://opentelemetry.io/docs/specs/semconv/general/recording-errors/)). So most healthy spans are Unset, not Ok. That surprises people who look for a green "Ok" in their backend.

## How do I set the error code on a span?

When an operation fails, instrumentation sets status Error and the `error.type` [attribute](https://hashspan.dev/learn/otel-span-attributes). [`error.type`](https://hashspan.dev/learn/otel-attributes/error#error.type) is stable and names the class of error, such as `timeout`. It should have low cardinality, so put the details somewhere else.

```ts
import { SpanStatusCode, type Span } from '@opentelemetry/api';

function endConfirm(span: Span, receipt: { status: 'success' | 'reverted' }) {
  span.setAttribute('blockchain.tx.status', receipt.status);
  if (receipt.status === 'reverted') {
    span.setAttribute('error.type', 'reverted');
    span.setStatus({ code: SpanStatusCode.ERROR });
  }
  span.end();
}
```

```text
confirm 8453 UNSET { 'blockchain.tx.status': 'success' }
confirm 8453 ERROR { 'blockchain.tx.status': 'reverted', 'error.type': 'reverted' }
```

## Whose error is it?

Status belongs to the operation the span describes, not to the whole trace. Here's the run:

Trace (withdraw_from_vault · from make demo, durations from the run; positions approximate):

- `execute_tool withdraw_from_vault`: 262.7 ms
  - `send 31337`: 4.4 ms
  - `confirm 31337`: 264.9 ms

The tool returned normally, so its span is Unset. The confirm span is Error. It ends after the tool because it stays open while the revert reason is fetched; the tool got the receipt before that.

- **`send` is Unset.** Sending worked: the node accepted the transaction and returned its hash.
- **`confirm` is Error.** The receipt says the transaction reverted.
- **`execute_tool` is Unset.** viem returned the receipt without throwing, and the tool passed `{ status: "reverted" }` back to the model. Nothing failed inside the tool.

You could argue the tool span should be Error too. I don't think so: the tool did its job and told the agent the truth. The failure is real, and it's on the span that describes it. Filter on `error.type` in your backend and you find it. [This post](https://hashspan.dev/blog/agent-trace-stops-at-the-tool-call) shows what you'd see without that span.

**In a hashspan trace:**
  | Situation | Span | Status | `error.type` |
  |---|---|---|---|
  | Hash returned | send | Unset | none |
  | Signing or broadcast failed | send | Error | library error code or class name |
  | Receipt: success | confirm | Unset | none |
  | Receipt: reverted | confirm | Error | `reverted` |
  | Gave up waiting | confirm | Error | `timeout` |
  | Replaced by another tx | confirm | Unset | none |

A replaced transaction isn't a failure of the wait: the receipt of the [replacing transaction](https://hashspan.dev/learn/tx-lifecycle) goes on that transaction's own confirm span.

## FAQ

### How do I set an error code on a span?

Call span.setStatus({ code: SpanStatusCode.ERROR }) and set the error.type attribute to the class of error, such as timeout or reverted. The status code says the span failed; error.type says how.

### When should a span have status Error?

When the operation it describes failed. Errors that were retried or handled, so the operation still completed, shouldn't mark the span as an error.

### What is error.type in OpenTelemetry?

A stable attribute that names the class of error the operation ended with, such as a status code or an exception class. It should have low cardinality. _OTHER is the fallback when the instrumentation has no better value.

### Should I set status Ok on successful spans?

Instrumentation libraries should leave status Unset on success. Ok is a final call by an application developer or operator, and it can't be overridden by a later Error.
