---
title: 'Run Studio: embedded, local binary or hosted'
description: 'Serve weft''s Inspector three ways: embedded in your Go app over the recorder''s own database (setup A), as the weft studio binary for any language (setup B), or hosted behind a token (setup C).'
url: https://weftgo.dev/docs/observe/studio/setup/
type: how-to
sources:
  - 'W11-fix3, v0.20.2: weft/cmd/weft/main.go:85-105 (one Studio per SQLite file: <db>.lock before any probe; refused, exit 1, weft dev reuses; ClickHouse takes no lock; the port policy), 633-668 (serveWith: lockDB, heldError, alreadyRunning on a reuse), 703-708 (noPlaygroundReason: drop the flag to turn the playground on), 925-940 (the db path made absolute), 364-370 (errors printed weft: <err>); weft/cmd/weft/dblock.go:15-25, 89-105 (heldError, alreadyRunning''s words); weft/internal/discovery/discovery.go:30-36 (otel skips the read under otel.Studio or otel.Local)'
  - weft/studio/studio.go:163-190 (DiscoveryFile, OffReason); weft/studio/example_test.go:162, 200 (ExampleDiscoveryFile, ExampleOffReason); weft/studio/README.md:57-76 (setup B for a non-Go app; the receiver OTLP/HTTP only); weft/README.md:375 — v0.20.2
  - weft/studio/README.md:1-9 (what Studio is), 11-55 (setup A, the loopback Host and cross-origin write rules), 57-109 (setup B), 150-203 (setup C), 3577-3582 (import path)
  - weft/studio/doc.go (the three setups; capabilities computed from the registered groups)
  - weft/AGENTS.md:215-229 (block 7, setup A, studio.New and its options), 402-411 (Token, AllowOrigins, the cross-origin write rule, IngestToken, X-Request-Id), 415-467 (setup B, the dev token, the discovery file, weft dev, the port policy)
  - weft/README.md:343-358 (Inspecting runs), 562-567 (studio/examples/basic, examples/studio-local)
  - 'weft/studio/studio.go:20 (Version), 25 (Option), 56 (DB: a nil db panics at New), 64 (Open), 71 (Live), 77 (NoIngest), 85 (IngestToken), 94 (Token), 106 (AllowOrigins), 114 (Base, default /studio/), 136 (Manifest), 141 (Title), 150 (Capabilities), 159 (Playground), 193 (Server), 247-295 (New: default path, the DB''s hub), 301 (Server.Handler), 313 (Handler), 321 (Runtime), 327 (Close), 397 (ServeHTTP: X-Request-Id)'
  - weft/studio/delete.go:131 (Retain; at least 1h, New panics below), 144-146 (retainFloorMsg)
  - weft/studio/studio.go:99-105 (AllowOrigins default with a Token), 130-135 (Manifest), 257-258 (the Retain floor), 335-340 (Close stops the runtime link), 350-384 (ingestAuthorized)
  - weft/studio/web/src/components/studio/app-shell.tsx:184-186 (Agents shows with a manifest or the playground capability)
  - weft/studio/pricing.go:352-356 (a WEFT_PRICING file that does not load panics at New)
  - weft/studio/internal/reqlog/reqlog.go:12-24 (refused-ingest lines rate-limited per minute; 5xx never)
  - 'weft/cmd/weft/main.go:875 (newServer sets Base and Token only: no ingest-token flag)'
  - weft/examples/studio-local/main.go:265-271 (8080 beside a running Studio)
  - weft/otel/pipeline.go:225-231 (an explicit otel.Studio switches the discovery read off)
  - 'weft/AGENTS.md:485 (block 7a: WEFT_ENV=dev or runtime.Enabled(true) opens the link), 400 (@weftgo/devtools: studio/web/npm, make devtools-npm, never published by the build)'
  - weft/studio/pricing.go:169 (Pricing)
  - 'weft/studio/auth.go:246-303 (the auth wrapper: ?token= refused, the loopback Host, cross-origin write, bearer), 794-849 (crossOriginWrite, hostAllowed)'
  - weft/studio/example_test.go:ExampleHandler, ExampleServer, ExampleRetain
  - weft/examples/studio-local/main.go:1-71, 226-311 (setup A with otel.Local and Playground)
  - weft/studio/examples/basic/main.go:1-12, 46-79 (demo runs, -serve, -addr, <dir>/dev.db)
  - weft/internal/discovery/discovery.go:1-40 (studio.json, its locations and trust rules)
  - weft/otel/pipeline.go:223-238, 332-351 (explicit and environment destinations combine; Install falls back to Local only with none)
  - weft/CHANGELOG.md:4541 (New, Close, Runtime, the options), 4629 (Handler over an obsdb.DB, Open, the default path), 5393 (studio/examples/basic)
  - weft/cmd/weft (weft --help, weft studio -h, weft dev -h at v0.17.0)
  - 'weft/cmd/weft/main.go:886-906 (newServer: only --db is parsed as a URL), 925-933 (dbFile: WEFT_DB as a plain path), 946-950 (dbLabel: the default banner label)'
  - 'observed: WEFT_DB=''clickhouse://u:fakepw@127.0.0.1:1/db'' weft studio (v0.17.0) started with no error, printed studio: token created in <cwd>/clickhouse:/u:fakepw@127.0.0.1:1/db.token, and /api/meta reported db.kind sqlite at that path'
  - docs-site/examples/studio/main_test.go:TestEmbeddedServesStudio, TestSetupARefusesCrossOriginWrite, TestSetupAHostRule, TestTokenWallsAPI, TestServerOptions
  - 'observed: weft studio v0.17.0 serves the UI at its root (/runs); go run github.com/weftgo/weft/studio/examples/basic@v0.20.2 prints studio: http://127.0.0.1:7331/studio/'
---

# Run Studio: embedded, local binary or hosted

Studio is weft's [Inspector](https://weftgo.dev/docs/reference/glossary/#inspector): a web UI, a JSON API, a live stream and an OTLP receiver over one recorded database, served as one `http.Handler` with the UI embedded. Run it one of three ways: embedded in your Go app ([setup A](https://weftgo.dev/docs/reference/glossary/#setup-a-b-c)), as the `weft studio` binary for apps in any language (setup B), or hosted behind a token (setup C).

For a Go app, setup A is two statements beside your own routes:

```go title="studio/main.go"
defer otel.Install(otel.Local(""))() // ./.weft/weft.db (or $WEFT_DB), content on

mux := http.NewServeMux()
mux.Handle("/studio/", http.StripPrefix("/studio",
	studio.Handler(studio.DB(otel.LocalDB()))))
```

[studio/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main.go)

Open `http://127.0.0.1:8080/studio/` (or wherever your mux listens) and the runs list shows every run the app records.

![weft Studio's runs list: each run's status, prompt and id, agent, steps, tokens, cost, start and duration, newest first, with a checkbox per row to pick runs to compare](https://weftgo.dev/docs/_astro/studio-runs.light.CFMu-KA1.webp)

*The runs list, the page Studio opens on.*

## The three setups

All three serve the same UI and API. They differ in who runs the server and what guards the API.

| Setup | Who runs it | Database | Guard on `/api` | Start it with |
|---|---|---|---|---|
| A · embedded | your Go app | the handle the recorder writes (`otel.LocalDB()`) | a loopback `Host`, and no cross-origin writes | [`studio.Handler`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Handler) or [`studio.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#New) |
| B · local binary | `weft studio` or `weft dev` | `sqlite://path` or `clickhouse://…` | the dev token | `weft studio` |
| C · hosted | your server | any `obsdb.DB` | `studio.Token`, and per-conversation panel tokens | `studio.New(…, studio.Token(tok))` |

Setup A is live with no network: writes go to the recorder's database handle, whose in-process hub feeds `/api/live`. Setup B receives OTLP over HTTP from any process on the same machine, so a Python or TypeScript app shows up beside your Go services. A non-Go app gets its traces, the runs derived from them and the devtools panel; the playground, breakpoints and approvals need a runtime link, which is Go only today.

The receiver is OTLP/HTTP only: `POST /v1/traces` and `POST /v1/logs` at the root, protobuf or JSON, with no gRPC and no metrics. Give an exporter the bare origin, and it adds `/v1/traces` and `/v1/logs` itself. Python's exporter defaults to gRPC and needs `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf`; the JavaScript one defaults to HTTP. A stock `invoke_agent` span makes a run, and `gen_ai.conversation.id` groups runs into a session ([Inspect agents written in other languages](https://weftgo.dev/docs/observe/other-languages/)). Setup C is setup A's handler behind a bearer token, for a Studio other people reach.

## Embed Studio

[`studio.DB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DB)`(`[`otel.LocalDB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#LocalDB)`())` serves the same [`obsdb.DB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#DB) handle the [local sink](https://weftgo.dev/docs/reference/glossary/#local-sink) writes. That shared handle is what makes setup A live: its hub publishes each write, and the live stream follows it.

Name the sink. [`otel.Install`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#Install)`(`[`otel.Local`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#Local)`(""))` keeps the local sink even when the environment adds a destination (`WEFT_STUDIO_URL`, `OTEL_EXPORTER_OTLP_ENDPOINT`, or a running `weft studio`'s discovery file). A bare `otel.Install()` adds the sink only when the environment names no destination; otherwise `otel.LocalDB()` is nil, and `studio.DB(nil)` panics when the server is built. [Your first agent](https://weftgo.dev/docs/get-started/first-agent/#5-record-the-runs-and-serve-studio) uses the same two lines.

The other ways to point Studio at a database:

- [`studio.Open(path)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Open) opens an obsdb SQLite file, creating it when missing. It panics when the file cannot be opened.
- With neither `DB` nor `Open`, Studio opens `$WEFT_DB`, else `./.weft/weft.db`. That second handle on the file reads history only: no live lane.

`studio.Handler(opts...)` is `studio.New(opts...).Handler()`. Use `studio.New` when you need to close the server or reach the playground's runtime link:

```go title="studio/main.go"
srv := studio.New(
	studio.DB(db),                  // the recorder's handle: history and the live lane
	studio.Retain(30*24*time.Hour), // delete runs last seen over 30 days ago
	studio.Pricing(studio.PricingTable{ // USD per 1M tokens, replaces the shipped table
		Dated:  "2026-10-10",
		Models: []studio.ModelPrice{{Provider: "acme", ModelGlob: "acme-*", Input: 1, Output: 4}},
	}),
	studio.Playground(true), // the playground routes and the runtime link
)
defer srv.Close() // stops the sweeper and the runtime link; a DB passed with DB stays open

mux := http.NewServeMux()
mux.Handle("/studio/", http.StripPrefix("/studio", srv.Handler()))
```

[studio/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main.go)

[`Server.Close`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.Close) stops [`Retain`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Retain)'s sweeper, shuts down the playground's runtime link server when `Playground(true)` built one, and closes the database only when `New` opened it (`Open`, or the default path). A handle you passed with `DB` stays yours. [`Server.Runtime`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.Runtime) is the runtime link server that `runtime.Local` takes; it is nil without `Playground(true)`.

Every option of [`studio.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#New) is an [`Option`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Option):

| Option | Default | Effect |
|---|---|---|
| [`DB(db)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DB) | `$WEFT_DB`, else `./.weft/weft.db` | The database to serve. A nil db panics in `New` |
| [`Open(path)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Open) | — | `DB(sqlite.Open(path))`, opened in `New` |
| [`Base(path)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Base) | `/studio/` | The path Studio is mounted at, with both slashes; written into the page's `<base href>` |
| [`Title(s)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Title) | `weft studio` | The page title |
| [`Manifest(json)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Manifest) | none | `weft.json` bytes for the agent and tool cards. Without one (and no runtime registering one) `/api/manifest` answers 404 and the Agents page is hidden, unless the playground is on: then Agents shows "No agents to show" until a runtime registers |
| [`Live(hub)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Live) | the DB's own hub, else one fed by ingest | Where `/api/live` reads frames |
| [`NoIngest()`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#NoIngest) | ingest on | Turns `/v1/traces` and `/v1/logs` off: a read-only Studio |
| [`IngestToken(tok)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#IngestToken) | loopback peers only | Requires `Authorization: Bearer <tok>` on OTLP ingest; once set, every ingest POST needs it, loopback included |
| [`Token(tok)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Token) | none | Walls the JSON API with a bearer token; keys the panel tokens |
| [`AllowOrigins(origins...)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#AllowOrigins) | none without a Token; with one, `localhost` and `127.0.0.1` on any port | CORS origins; without a Token, also the extra `Host`s the API answers |
| [`Capabilities(names...)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Capabilities) | none | Capability names a hosting wrapper adds to `/api/meta` |
| [`Playground(on)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Playground) | off | The playground routes and the runtime link server |
| [`Retain(d)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Retain) | off | Deletes runs not running and last seen more than `d` ago, once at `New` and then every `min(d/10, 1h)` (at least a minute apart). `d` must be at least `1h`: `New` panics below it, since a run silent for 30 s reads interrupted and would be swept |
| [`DiscoveryFile(path)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DiscoveryFile) | unset | Since 0.20.0: the discovery file the hosting command writes for this server (`weft studio` and `weft dev` set it). `/api/meta` serves it as `discovery_file`, under `db.path`'s rule, while the file names this process. An embedded Studio writes no such file and leaves it unset |
| [`OffReason(capability, why)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#OffReason) | the Go option's words | Since 0.20.0: your words for why `capabilities_off` says a capability is off. They replace the reason of every capability off for the same cause. `weft studio --no-playground` says "weft studio was started with --no-playground: drop the flag to turn the playground on". Words only; naming a capability that is not an optional one panics in `New`. [`ExampleOffReason`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#example-OffReason) and [`ExampleDiscoveryFile`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#example-DiscoveryFile) show both |
| [`Pricing(table)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Pricing) | `$WEFT_PRICING`, else the shipped table | The table usage is priced with, replacing the default whole. A `WEFT_PRICING` file that does not load makes `New` panic, naming the path and the field |

`/api/meta` reports what the server does: its `capabilities` list is computed from the route groups it registered (`live`, `ingest`, `search`, `playground` and others), its `retention` from `Retain`, its `pricing.source` (`default`, `file` or `option`) from `Pricing`. The UI shows a screen only when its capability is listed. [Keep and delete recorded runs](https://weftgo.dev/docs/production/retention/) covers `Retain`; [See what a run cost](https://weftgo.dev/docs/observe/studio/cost/) covers pricing; [Connect your app to Studio's playground](https://weftgo.dev/docs/observe/playground/connect/) covers `Playground(true)`.

### Who setup A answers

Without a `Token`, the whole `/api` tree answers only a request whose `Host` is loopback: `localhost`, `*.localhost`, `127.0.0.0/8` or `[::1]`, any port. A DNS-rebinding page (`http://evil.example:7331` resolving to 127.0.0.1) gets a 403 instead of your transcripts. `X-Forwarded-Host` and `Forwarded` are never trusted; behind a proxy, the `Host` it forwards is the one checked.

Setup A also refuses a cross-origin write: an unsafe method (anything but GET, HEAD or OPTIONS) whose `Origin` is not Studio's own and not an `AllowOrigins` entry, or one with no `Origin` but `Sec-Fetch-Site: cross-site`. The answer is a 403 that starts `cross-origin write refused (setup A)`. Same-origin pages and clients that send neither header (`curl`, the runtime link) go through.

An app served on a real hostname without a token lists its origin, whose `host:port` must equal the request's `Host`:

```go title="studio/main.go"
mux := http.NewServeMux()
mux.Handle("/studio/", http.StripPrefix("/studio", studio.Handler(
	studio.DB(db),
	studio.AllowOrigins("http://myapp.internal:8080"),
)))
```

[studio/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main.go)

The UI shell and `/panel.js` carry no data and are served to any `Host`. `/panel.js` is the devtools panel, which your own pages load from Studio ([Install the devtools panel](https://weftgo.dev/docs/observe/devtools/install/)). The same bytes are packaged for npm as `@weftgo/devtools` (`make devtools-npm` in weft's repository), but the package is not published to the registry.

## Setup A in one program

weft's [`examples/studio-local`](https://github.com/weftgo/weft/tree/v0.20.2/examples/studio-local) is setup A whole: `otel.Install(otel.Local(""), otel.Slog())`, `studio.New(studio.DB(otel.LocalDB()), studio.Playground(true))` mounted under `/studio/`, a thread session whose turns appear live, the devtools panel on its landing page, and `runtime.Install(runtime.Local(srv), …)` so the playground drives its agent. It runs offline on a deterministic model:

```sh
go run ./examples/studio-local
```

Run it from a clone of weft at `v0.20.2`. It listens on `127.0.0.1:7331` (on `127.0.0.1:8080` when `WEFT_STUDIO_URL` or a running `weft studio`'s discovery file names a Studio, which then holds 7331) and serves Studio at `/studio/`; `curl -s -XPOST localhost:7331/run -d 'where is order 42?'` starts a turn. Its agent's `PrepareStep` trims a paragraph from the system prompt after step 0, which the run page's Request pane shows as "changed by PrepareStep".

## Run the weft studio binary

Setup B is the `weft` command ([Install the weft command](https://weftgo.dev/docs/get-started/install/#install-the-weft-command)). With no flags it serves the UI, OTLP ingest, the playground and a dev token on `127.0.0.1:7331`, over `$WEFT_DB` or `./.weft/weft.db`:

```sh
weft studio
weft studio --db sqlite://runs.db --retain 720h
weft studio --db clickhouse://user:pass@host:9000/db --addr 127.0.0.1:7400
```

The binary serves the UI at its root, so the runs list is `http://127.0.0.1:7331/runs`. Its flags:

| Flag | Env var | Default | Effect |
|---|---|---|---|
| `--addr` | `WEFT_STUDIO_ADDR` | `127.0.0.1:7331` | Listen address; set, it pins: a busy address is an error |
| `--db` | `WEFT_DB`, a bare SQLite path | `./.weft/weft.db` | `sqlite://path` or `clickhouse://user:pass@host:9000/db`. Only the flag takes a URL |
| `--token` | `WEFT_STUDIO_TOKEN` | the database's `<db>.token` | The API token |
| `--rotate-token` | — | — | Writes a new `<db>.token` before serving |
| `--manifest` | `WEFT_MANIFEST` | the nearest `weft.json` upward | The manifest for the agent and tool cards |
| `--open` | — | on when stdout is a terminal | Opens the browser with the token in the URL fragment |
| `--no-playground` | — | playground on | Turns the playground off |
| `--retain` | `WEFT_RETAIN` | off | Retention, a Go duration of at least `1h`; less (`--retain 30m`) is a usage error, exit 2 |
| `--pricing` | `WEFT_PRICING` | the shipped table | A pricing file that replaces the table; a file that does not load is exit 1, naming the path and the field |
| `--log-level` | `WEFT_LOG_LEVEL` | `info` | `debug`, `info`, `warn` or `error` |
| `--log-format` | `WEFT_LOG_FORMAT` | `text` when a level is set, otherwise Go's plain log line | `text` or `json` |

**The dev token.** It is stable per database: `weft studio` keeps it in `<db>.token` beside the SQLite file, mode 0600, and never prints it. `--open` hands it to the browser in the URL fragment, which never reaches a server; any other browser asks for it once ("This Studio needs its API token") and keeps it. A database with no file gets a generated token, printed at start.

**The discovery file.** Once the listener is bound, `weft studio` writes `studio.json` (url, token, db, pid, start time, version; mode 0600) in the first of `./.weft/` when it exists, `$XDG_RUNTIME_DIR/weft/`, or the user cache directory, and removes it on exit. A Go app's `otel.Install()` reads it when `WEFT_STUDIO_URL` is unset and the pipeline names no `otel.Studio(…)` or `otel.Local`, and exports to that Studio with no configuration. `runtime.Install()` reads it too, but opens its link only under `WEFT_ENV=dev` or `runtime.Enabled(true)`. A file is trusted only when its URL is loopback, its pid is alive, it is under 24 hours old and, on unix, it is 0600 and yours. `WEFT_STUDIO_URL` always wins; `WEFT_DISCOVERY=off` turns the read off and, since 0.18.0, the write: `weft studio` then writes no file and says so in one start line ([Environment variables](https://weftgo.dev/docs/reference/env/#weft-discovery)).

**One Studio per database file.** Since 0.20.0 `weft studio` takes `<db>.lock`, an exclusive OS lock, before it probes any port, and holds it while it serves. The kernel releases it when the process ends, however it ends. A second `weft studio` on the same SQLite file is refused with exit 1, before it opens the database or a listener. It prints on stderr:

```text
weft: studio already running at http://127.0.0.1:7331 (pid 1234) on /home/you/.weft/weft.db: one Studio per database file — sign in to it with `weft open --with-token`, or stop it first
```

`weft dev` reuses the running Studio instead. A ClickHouse database is a shared server and takes no lock.

**The port.** Anything else on 7331 moves Studio to the next free port in 7331–7340, said in one line. That covers another program, a Studio on another database, or one whose meta this token cannot read. `--addr` pins the address instead.

An app in another language exports OTLP over HTTP to the same port; Studio ingests from loopback without a token. The binary has no ingest-token flag, so setup B takes OTLP from this machine only. Python's OTLP exporter defaults to gRPC, so ask for HTTP:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:7331 OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf python app.py
```

`weft dev` runs Studio in-process and your app beside it, restarting the app on a `.go` save; it takes the same flags plus `--no-watch` and `--watch dir`:

```sh
weft dev
weft dev -- go run ./cmd/app
```

[Develop with weft dev](https://weftgo.dev/docs/observe/weft-dev/) covers it, and [Inspect agents written in other languages](https://weftgo.dev/docs/observe/other-languages/) what Studio derives from a non-weft app's spans.

## Host Studio behind a token

Setup C is the same handler with [`studio.Token`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Token). Every `/api` request then needs `Authorization: Bearer <token>`, and the `Host` no longer matters:

```go title="studio/main.go"
mux := http.NewServeMux()
mux.Handle("/studio/", http.StripPrefix("/studio", studio.Handler(
	studio.DB(db),
	studio.Token(apiToken),          // every /api route: Authorization: Bearer <apiToken>
	studio.IngestToken(ingestToken), // /v1/traces and /v1/logs: Authorization: Bearer <ingestToken>
)))
```

[studio/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main.go)

A token in the URL (`?token=`) is refused on every `/api` route, in every setup: the answer is a 401 that starts `a token in the URL is never accepted`. A browser's live stream opens with a short-lived signature instead ([Watch runs live](https://weftgo.dev/docs/observe/studio/live/#open-the-stream-yourself)).

OTLP ingest has its own rule. Without [`IngestToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#IngestToken) it answers loopback peers only, and refuses even those when the request carries `Forwarded`, `X-Forwarded-For` or `X-Real-IP`, or a browser `Origin` that is not allowed; so a Studio bound wide refuses remote exporters until you set one. Once it is set, every ingest request needs `Authorization: Bearer <ingestToken>`, loopback included. Your backend mints per-conversation panel tokens through `POST /api/panel-tokens` for the devtools panel; [Secure Studio](https://weftgo.dev/docs/production/security/) covers them, TLS and the proxy rules.

Every response carries an `X-Request-Id` (the client's, else a generated one; [`Server.ServeHTTP`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.ServeHTTP)), and a 5xx or a refused ingest batch is one line on `slog.Default` naming it (refused-ingest lines at most one a minute per remote, route and status; 5xx lines are never limited) ([Operate Studio](https://weftgo.dev/docs/production/studio-operations/)).

## Demo runs

To see Studio with data before you record any, run weft's `studio/examples/basic`. It records a tool-call run, a subagent run and a failing run into `<dir>/dev.db` (default `.weft`), then serves Studio on `127.0.0.1:7331`:

```sh
go run github.com/weftgo/weft/studio/examples/basic@v0.20.2
```

```text
studio: http://127.0.0.1:7331/studio/
```

`-serve` skips recording and only serves; `-addr` picks the address. It writes obsdb batches directly, where a real app records the same rows through `weft/otel`.

## Import path

`github.com/weftgo/weft/studio` is a package of the framework module: `go get github.com/weftgo/weft` brings it, at the same version as every other layer. [`studio.Version`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Version) is that module version, reported as `/api/meta`'s `studio_version`. The package imports `core` and `obsdb` (its SQLite backend among them) and never the ClickHouse backend: within weft, only the `weft` binary (`cmd/weft`) imports `obsdb/clickhouse`. A setup-C server of your own may import it too.

## Complete example

The example's `main.go` records one scripted run and serves Studio in setup A; its other functions are the `studio.New`, token and hostname variants above:

```go title="studio/main.go"
// Command studio is the "Run Studio" page's program: setup A, Studio
// embedded beside the app over the recorder's own database handle, plus the
// studio.New form with its options, a token-walled Studio and an app served
// on a real hostname.
//
// main runs setup A: it records one run of a scripted support agent, then
// serves Studio at http://127.0.0.1:8080/studio/. The other functions are
// the page's variants; main_test.go runs each of them offline.
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/weftgo/weft"
	"github.com/weftgo/weft/obsdb"
	"github.com/weftgo/weft/otel"
	"github.com/weftgo/weft/studio"
	"github.com/weftgo/weft/wefttest"
)

// LookupInput is lookup_order's input.
type LookupInput struct {
	OrderID string `json:"order_id" jsonschema:"the order to look up"`
}

// newAgent is a support agent with one tool, on a scripted model so the
// program runs offline.
func newAgent() *weft.Agent {
	lookup := weft.Tool("lookup_order", "Look up an order by ID.",
		func(ctx context.Context, in LookupInput) (string, error) {
			return "order " + in.OrderID + ": shipped", nil
		})
	model := wefttest.Script(
		wefttest.ToolCalls(wefttest.Call{ID: "c1", Name: "lookup_order", Args: `{"order_id":"1234"}`}),
		wefttest.Say("Order 1234 has shipped."),
	)
	return weft.New(model, weft.Name("support-bot"), lookup)
}

// run is setup A: record every run into the local sink and serve Studio
// over the same handle, so the UI reads what the runs wrote, live.
func run(ctx context.Context, serve func(http.Handler) error) error {
	defer otel.Install(otel.Local(""))() // ./.weft/weft.db (or $WEFT_DB), content on

	mux := http.NewServeMux()
	mux.Handle("/studio/", http.StripPrefix("/studio",
		studio.Handler(studio.DB(otel.LocalDB()))))

	if _, err := newAgent().Generate(ctx, weft.Prompt("Where is order 1234?")); err != nil {
		return err
	}
	return serve(mux)
}

// serveServer is the studio.New form: options, then Handler, then Close.
func serveServer(db obsdb.DB, serve func(http.Handler) error) error {
	srv := studio.New(
		studio.DB(db),                  // the recorder's handle: history and the live lane
		studio.Retain(30*24*time.Hour), // delete runs last seen over 30 days ago
		studio.Pricing(studio.PricingTable{ // USD per 1M tokens, replaces the shipped table
			Dated:  "2026-10-10",
			Models: []studio.ModelPrice{{Provider: "acme", ModelGlob: "acme-*", Input: 1, Output: 4}},
		}),
		studio.Playground(true), // the playground routes and the runtime link
	)
	defer srv.Close() // stops the sweeper and the runtime link; a DB passed with DB stays open

	mux := http.NewServeMux()
	mux.Handle("/studio/", http.StripPrefix("/studio", srv.Handler()))
	return serve(mux)
}

// hosted is a Studio that may listen beyond loopback: a bearer token walls
// the API, and OTLP ingest takes its own token.
func hosted(db obsdb.DB, apiToken, ingestToken string) http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/studio/", http.StripPrefix("/studio", studio.Handler(
		studio.DB(db),
		studio.Token(apiToken),          // every /api route: Authorization: Bearer <apiToken>
		studio.IngestToken(ingestToken), // /v1/traces and /v1/logs: Authorization: Bearer <ingestToken>
	)))
	return mux
}

// onHostname is setup A for an app served on a real hostname without a
// token: the origin is listed, so its Host is answered besides loopback.
func onHostname(db obsdb.DB) http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/studio/", http.StripPrefix("/studio", studio.Handler(
		studio.DB(db),
		studio.AllowOrigins("http://myapp.internal:8080"),
	)))
	return mux
}

func main() {
	serve := func(h http.Handler) error {
		fmt.Println("Studio: http://127.0.0.1:8080/studio/")
		return http.ListenAndServe("127.0.0.1:8080", h)
	}
	if err := run(context.Background(), serve); err != nil {
		log.Fatal(err)
	}
}
```

[studio/main.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main.go)

[View on GitHub](https://github.com/weftgo/weftgo.dev/tree/main/docs-site/examples/studio) · run it from `docs-site/examples`:

```sh
go run ./studio
go test ./studio
```

```text
Studio: http://127.0.0.1:8080/studio/
```

The tests in [`main_test.go`](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/studio/main_test.go) check that `/studio/api/meta` answers and lists the run, that a cross-origin POST and a foreign `Host` are refused under setup A, that a Token walls the API and refuses `?token=`, and that `Retain`, `Pricing` and `Playground(true)` show in `/api/meta`. The package's own examples: [`ExampleHandler`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#example-Handler) mounts `studio.Handler(studio.DB(db))` under `/studio/` over a SQLite handle and reads `/studio/api/meta` (200); [`ExampleServer`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#example-Server) builds `studio.New(studio.Open(path), studio.NoIngest())`, takes its `Handler()`, shows `Runtime()` nil without `Playground(true)`, and closes it.

## Best practices & gotchas

- **Name the local sink when you embed Studio.** `otel.Install()` alone writes no local sink when the environment names a destination, and `studio.DB(nil)` panics: `studio: New called with a nil DB`. (Source: `otel/pipeline.go:332-351`; `studio/studio.go:255`)
- **Pass the recorder's handle, not a path.** `studio.Open` or the default path opens a second handle: history works, live does not. (Source: `studio.DB` godoc)
- **Build a Studio with `Retain` or `Playground(true)` through `New`, and close it.** Nothing can close the server `Handler` builds; its sweeper stops only when the database closes. (Source: `studio.Handler` and `studio.Retain` godoc)
- **Set a Token before Studio listens beyond loopback.** Without one, the API's only guard is the loopback `Host`; `AllowOrigins` widens it to the hosts you list. (Source: `studio.Token` godoc; `studio/auth.go:246-303`; the example's `TestTokenWallsAPI`)
- **Configure ClickHouse with `--db`, not `WEFT_DB`.** `WEFT_DB` is a bare SQLite path, so a container that sets `WEFT_DB='clickhouse://…'` gets an empty SQLite Studio under a directory named after the URL, password included, with no error ([weft CLI reference](https://weftgo.dev/docs/reference/cli/#notes)). (Source: `cmd/weft/main.go:886-906, 925-933`; observed with the v0.20.2 binary)
- **Behind a proxy that rewrites `Host` (Vite's `changeOrigin: true`), list the front origin in `AllowOrigins`.** Setup A compares the `Origin`'s `host:port` with the `Host`. (Source: `studio/README.md:38-45`)

## See also

- [Find runs: the runs list, search and filters](https://weftgo.dev/docs/observe/studio/runs/)
- [Record every run with otel.Install](https://weftgo.dev/docs/observe/recording/)
- [Develop with weft dev](https://weftgo.dev/docs/observe/weft-dev/)
- [Secure Studio](https://weftgo.dev/docs/production/security/)
- [Environment variables](https://weftgo.dev/docs/reference/env/)

Symbols used: [`studio.Handler`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Handler) · [`studio.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#New) · [`studio.Server`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server) · [`Server.Handler`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.Handler) · [`Server.Close`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.Close) · [`Server.Runtime`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.Runtime) · [`Server.ServeHTTP`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Server.ServeHTTP) · [`studio.Option`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Option) · [`studio.DB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DB) · [`studio.Open`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Open) · [`studio.Base`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Base) · [`studio.Title`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Title) · [`studio.Manifest`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Manifest) · [`studio.Live`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Live) · [`studio.NoIngest`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#NoIngest) · [`studio.IngestToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#IngestToken) · [`studio.Token`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Token) · [`studio.AllowOrigins`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#AllowOrigins) · [`studio.Capabilities`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Capabilities) · [`studio.Playground`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Playground) · [`studio.Retain`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Retain) · [`studio.Pricing`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Pricing) · [`studio.PricingTable`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#PricingTable) · [`studio.ModelPrice`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#ModelPrice) · [`studio.Version`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Version) · [`otel.Install`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#Install) · [`otel.Local`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#Local) · [`otel.LocalDB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#LocalDB) · [`obsdb.DB`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#DB) · [`weft.Tool`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Tool) · [`weft.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#New) · [`weft.Name`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Name) · [`weft.Prompt`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Prompt) · [`Agent.Generate`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Generate) · [`wefttest.Script`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/wefttest#Script)
