GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - braintrustdata/braintrust-sdk-rust

BranchesTags
Open more actions menu

Latest commit

 

History

111 Commits
111 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
crates/braintrust-api-client
crates/braintrust-api-client
 
 
examples
examples
 
 
hooks
hooks
 
 
scripts
scripts
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
Cargo.toml
Cargo.toml
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
braintrust-logo.svg
braintrust-logo.svg
 
 

Repository files navigation

Braintrust

braintrust-sdk-rust

Rust SDK for Braintrust logging and tracing.

Early Development Notice: This SDK is in early development (alpha). Expect backwards-incompatible changes between releases until we reach 1.0.

Installation

Add this to your Cargo.toml:

[dependencies]
braintrust-sdk-rust = "0.1.0-alpha.1"

Usage

use braintrust_sdk_rust::{BraintrustClient, BraintrustClientConfig, SpanLog};
use serde_json::json;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create a client pointing to your Braintrust instance
    let client = BraintrustClient::new(
        BraintrustClientConfig::new("https://api.braintrust.dev")
    )?;

    // Create a span for logging
    let span = client
        .span_builder("your-api-token", "your-org-id")
        .project_name("my-project")
        .build();

    // Log input/output using span.log() with the builder pattern
    span.log(
        SpanLog::builder()
            .input(json!({"prompt": "Hello, world!"}))
            .output(json!({"response": "Hi there!"}))
            .build()
            .expect("Failed to build SpanLog")
    ).await;

    // Flush the span data to Braintrust
    span.flush().await?;
    client.flush().await?;

    Ok(())
}

Features

  • Span-based logging: Create spans to track LLM calls and other operations
  • Usage metrics extraction: Built-in extractors for OpenAI and Anthropic usage metrics
  • Async-first: Built on Tokio for high-performance async operations
  • Background submission: Logs are submitted in the background to minimize latency

Generated REST API client

The SDK includes a typed REST API client at braintrust_sdk_rust::api. See the API client README for usage.

Preserving existing span provenance

For updates that must not replace an existing span's origin, build the update with SpanLog::builder().skip_span_origin(). This omits automatically generated context.span_origin fields, including configured SDK origin and environment overrides. Explicit or propagated context remains unchanged; callers preserving an existing origin should not supply a replacement in that context.

When logged to a SpanHandle, the option remains enabled through subsequent logs and end(). It does not delete provenance already stored on the backend.

Private CA bundles

When outbound HTTPS traffic passes through a TLS-inspecting proxy, set BRAINTRUST_CUSTOM_CA_BUNDLE to the PEM contents of the additional CA certificate bundle. The SDK appends these certificates to its normal public trust store and continues to use the standard HTTP_PROXY, HTTPS_PROXY, and NO_PROXY behavior.

For programmatic configuration, use BraintrustClient::builder().custom_ca_bundle_pem(pem). The existing .ca_bundle(path) method remains available when the PEM bundle is stored in a file.

Extracting Usage Metrics

The SDK includes helpers for extracting usage metrics from provider responses:

use braintrust_sdk_rust::{extract_openai_usage, extract_anthropic_usage};
use serde_json::json;

// Extract from OpenAI response
let openai_response = json!({
    "usage": {
        "prompt_tokens": 100,
        "completion_tokens": 50,
        "total_tokens": 150
    }
});
let usage = extract_openai_usage(&openai_response);

// Extract from Anthropic response
let anthropic_response = json!({
    "usage": {
        "input_tokens": 100,
        "output_tokens": 50
    }
});
let usage = extract_anthropic_usage(&anthropic_response);

Organizations and projects

Use project(name) to log to a specific project. To work in another organization that your API key can access, call with_organization(name) and select a project from that client.

# use braintrust_sdk_rust::BraintrustClient;
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = BraintrustClient::builder().api_key("your-key").build().await?;
let other_org = client.with_organization("another-org").await?;
let evaluations = other_org.project("evaluations");

let span = evaluations.span_builder().await?.build();
span.end();
evaluations.flush().await?;
# Ok(())
# }

A project handle's flush() waits for writes through that handle. Flushing its client also covers writes through the client's project handles.

Service configuration

Call configure_service before creating your first SDK client or bulk uploader. Use ServiceConfig to set process-wide request concurrency, limit how many pending rows the SDK holds across all clients or per organization, or select the Tokio runtime for SDK work.

use braintrust_sdk_rust::{configure_service, ServiceConfig};

configure_service(ServiceConfig::builder()
    .max_concurrent_requests(8)
    .max_rows(100_000)
    .max_org_rows(10_000)
    .build())?;
# Ok::<(), braintrust_sdk_rust::BraintrustError>(())

License

This project is licensed under the Apache License, Version 2.0.

About

No description, website, or topics provided.

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

4 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.