Skip to main content
Braintrust experts can be invoked programmatically via REST API, enabling automated pipelines, CI/CD integrations, custom applications, and webhook-driven workflows.

Base URL

Authentication

All API requests require an API key passed as the x-api-key header.
Get your API key from Settings → Manage API Keys. See Managing API Keys for a step-by-step guide.
Never commit API keys to source control or log them in plain text. Treat them like passwords.

Invoke an Expert

POST /api/agents/invoke Send a message to an expert and receive a response. Supports streaming (default) and async job modes.

Request Body

* Use either agent or agents, never both.
Fields async and wait_for_completion are not supported — use stream only.

Attachments

Each entry in the attachments array is an object with the following fields: No other fields are accepted — extra keys return 400. Maximum 20 attachments per invoke.
Files must be uploaded before invoking the expert. Use POST /api/agents/invoke/attachments/upload-url to obtain a presigned URL, upload the file to that URL, then include the returned attachment object (s3_key, type, name) in your invoke request. The s3_key must start with invoke-uploads/ — arbitrary S3 paths are rejected with 400.
JSON request body example with attachments:
curl example:
Get a presigned upload URL
Call this endpoint once per file before invoking with attachments. POST /api/agents/invoke/attachments/upload-url Request fields: Response fields:
Response:
Reuse the same uploadSessionId across multiple files in one batch. Call upload-url once per file; each response gives a unique s3Key.

Streaming Mode (Default)

When stream is omitted or true, the response is a Server-Sent Events (SSE) stream at HTTP 200 with Content-Type: text/event-stream. Lines beginning with data: are JSON payloads. Lines beginning with : keepalive are heartbeat comments — ignore them when parsing. Streaming responses include an X-Request-Id response header containing the server-generated request correlation ID.
Log X-Request-Id from every streaming response alongside your task_id and thread_token. If a request behaves unexpectedly, the X-Request-Id value lets support engineers locate the exact server-side trace without needing full request payloads.

Event sequence

1. meta — always the first event
2. chunk — zero or more token events
3. done — terminal success Single agent:
Multi-agent collaboration:
On failure:
Every successful invoke turn gets a UUID request_id echoed in meta, done, async 202, poll responses, and the X-Request-Id response header. Save it to look up the LangSmith trace timeline (see Traces).

Example

If you see metachunk lines → done, your integration is working. Save the thread_token from meta or done for follow-up messages.

Async Mode (stream: false)

When stream is false, the server returns 202 Accepted immediately with a task_id you can poll.

Step 1 — Create the async task

Response:

Step 2 — Poll for the result

Prepend the base URL to status_url to get the full polling URL. Possible status values: queued, running, completed, failed, timeout, canceled

Step 3 — Cancel (optional)

Cancel any queued or running task (including a streaming run, using its task_id from meta):
Cancel is best-effort. On success you get 200:
If the task is already in a terminal state, you still get 200 but with cancel_applied: false and a reason field: Other statuses:

Thread Continuity

thread_token keeps messages connected into a single conversation thread.
If you change the agents array while reusing the same thread_token, the conversation will not continue from the previous thread. Keep the same agent roster across follow-up messages to maintain continuity.

Multi-turn example


Multi-Agent Collaboration

Use the agents array (not agent) to orchestrate multiple experts in a single turn.
  • The done event and poll responses include agents_used (display names of agents that contributed), not agents.
  • Keep the same agents roster and thread_token on follow-ups for continuity.

Delivery Options

Deliver to a Google Chat Space

Requirement: the BrainTrust bot must be a member of that space.

Deliver as DM from the agent

Delivery diagnostics

Streaming done events may include delivery_error (string) when posting to the configured destination failed, or delivered_to (object) confirming where the result was sent. Polled GET task-status responses do NOT carry a separate delivery_error field on success — use streaming if you need delivery diagnostics in the HTTP body.

Read Endpoints

Task status

GET /api/agents/invoke/{task_id} Retrieve status and output for any invocation (streaming or async) by task_id. The task_id is returned in the streaming meta event and in the async 202 response.
Response example:

Invocation history

GET /api/invocations
Query parameters: Response shape: { "invocations": [...] } — each item includes task_id, status, thread_token, mode, input, output, and timestamps.

Traces and Debugging

Every invoke turn returns a UUID request_id (in meta/done, the async 202 body, poll responses, and the X-Request-Id response header). Use it to fetch the LangSmith trace timeline for that turn. Same x-api-key as invoke; you must have hired the expert. GET /api/agents/{agent_id}/traces/{request_id}/timeline Returns run header fields (name, status, start_time, end_time, error) and an interleaved timeline of model and tool events. When truncated is true, the trace exceeded internal fetch limits.
LangSmith indexing can lag a few seconds after invoke completes — retry if you get 404 immediately after done.

Error Responses


Windows PowerShell

Use curl.exe — PowerShell’s built-in curl is an alias for Invoke-WebRequest and is not compatible.

Best Practices

  • Start with streaming mode (-N flag) and parse the initial meta event before any chunk lines.
  • Move to async mode only when your system needs job-style polling.
  • Keep agent IDs lowercase with hyphens.
  • Add context for better, more relevant answers.
  • Save task_id for traceability and thread_token for conversation continuity.
  • On multi-turn flows, always send the current thread_token, not a stale one from a prior segment.

Next Steps

Managing API Keys

Create and manage your API keys.

Triggers

Automate expert invocations on a schedule.