> ## Documentation Index
> Fetch the complete documentation index at: https://braintrust.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Large trace download fails with 'Invalid string length'

export const plans_0 = "Any"

export const deployments_0 = "Any"

export const data_plane_version_0 = undefined

export const use_case_0 = "Use case - Downloading a single large trace via the UI 'Download this trace' option when it fails silently or throws 'Invalid string length'"

<Note>
  **Applies to:**

  * Plan - {plans_0}
  * Deployment - {deployments_0}
  * {data_plane_version_0}
  * {use_case_0}
</Note>

## Summary

**Issue:** The **Download this trace** option in the UI can fail for very large traces, sometimes showing an `Invalid string length` error.

**Cause:** The UI fetches the trace spans through `/btql` and then serializes the full trace payload to JSON for download. For very large traces, that serialization can exceed the JavaScript runtime’s maximum string size.

**Resolution:** Fetch the trace spans through the Braintrust API with pagination instead of using the UI download button.

## Diagnosing the failure

Open browser DevTools and check the Network and Console tabs when the download fails. An `Invalid string length` error is consistent with a very large trace payload failing during JSON serialization.

## Resolution steps

### Fetch the trace via SQL

Use SQL to retrieve spans for the trace with full payloads and stable ordering:

```sql theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
SELECT *
FROM project_logs('<PROJECT_ID>', shape => 'spans')
WHERE root_span_id = '<ROOT_SPAN_ID>'
ORDER BY _pagination_key ASC
LIMIT 100
SETTINGS preview_length = -1;
```

If pagination is required, fetch the next page with the [returned cursor](https://www.braintrust.dev/docs/reference/sql#cursors-for-pagination):

```sql theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
SELECT *
FROM project_logs('<PROJECT_ID>', shape => 'spans')
WHERE root_span_id = '<ROOT_SPAN_ID>'
ORDER BY _pagination_key ASC
LIMIT 100
OFFSET '<CURSOR_FROM_PREVIOUS_RESPONSE>'
SETTINGS preview_length = -1;
```

### Reduce trace size at logging time

If large inline payloads are causing repeated issues, move large data to attachments to reduce the size of span payloads during logging and export. See "[Optimize large trace logging with attachments](https://www.braintrust.dev/docs/kb/optimize-large-trace-logging-with-attachments)" troubleshooting article.

## Notes

* Filtering by `root_span_id` is an efficient way to retrieve a known trace.
* `_pagination_key` provides a stable ordering for pagination.
