Skip to main content
Custom providers allow you to integrate any AI model API with the Circuit Breaker Labs CLI using Rhai scripting. This enables safety testing for proprietary models, internal deployments, or any non-standard API.

What is Rhai?

Rhai is a simple, embedded scripting language designed for Rust applications. It has a JavaScript-like syntax and is used by the CLI to translate between the standard Circuit Breaker Labs message format and your custom API’s format.
You don’t need to be a Rhai expert to create custom providers. The examples below cover all common use cases.

Why Rhai for Custom Providers?

Sandboxed Execution

Scripts run in a secure sandbox with no file system or network access

Simple Syntax

JavaScript-like syntax that’s easy to learn and read

Type Safety

Strong typing prevents runtime errors

Fast Performance

Compiled to bytecode for efficient execution

How Custom Providers Work

Custom providers act as translators between the CLI and your API:
1

CLI Prepares Messages

The CLI generates conversation messages in a standard format:
2

build_request() Transforms

Your Rhai script’s build_request() function converts these messages into your API’s request format.
3

CLI Posts Request

The CLI sends the transformed request to your specified URL endpoint.
4

parse_response() Extracts

Your script’s parse_response() function extracts the assistant’s message from the API response.
5

Evaluation Continues

The CLI uses the extracted message for safety evaluation and continues the conversation if needed.

Script Structure

Every custom provider script must implement two functions:
Both functions are required. The CLI will fail if either is missing or has the wrong signature.

Complete Examples from Source

The CLI repository includes working examples for common API formats:

OpenAI Chat Completions API

Usage:

OpenAI Responses API

Ollama Chat API

Usage:

Ollama Completions API

Creating Your Own Provider

1

Identify Your API Format

Determine what request format your API expects and what response format it returns. Test with curl:
2

Create Rhai Script

Create a .rhai file with build_request() and parse_response() functions:
3

Test the Script

Run a simple evaluation to verify the script works:
4

Iterate and Refine

Check error messages for issues with request/response parsing and adjust your script accordingly.

Advanced Examples

Adding Custom Parameters

Handling Different Message Formats

If your API expects a different message structure:

Concatenating Messages for Completion APIs

Some APIs expect a single prompt instead of structured messages:

Handling Nested Response Structures

Adding Debug Logging

Debug output appears in the CLI logs. Use this to troubleshoot request/response issues.

Rhai Quick Reference

Data Types

Control Flow

Common Operations

Functions

Authentication and Headers

Authentication is typically handled via HTTP headers passed from environment variables:
The CLI automatically includes standard headers. Your API key should be configured according to your API’s authentication requirements (Bearer token, API key header, etc.).
If you need custom headers, they can be set at the HTTP client level. Contact the Circuit Breaker Labs team if you need advanced header customization.

Troubleshooting

Your script is missing the build_request function. Ensure it’s defined:
Your script is missing the parse_response function. Ensure it’s defined:
The response structure doesn’t match your parsing logic. Add debug logging:
  • Verify your API URL is correct
  • Check authentication headers are set properly
  • Ensure request format matches what your API expects
  • Test with curl to confirm API access
Your parse_response logic might be extracting the wrong field. Log the full response:

Testing Your Custom Provider

1

Test with Single Variation

Start with minimal settings to quickly identify issues:
2

Verify Request Format

Check logs to ensure requests match your API’s expected format. Add print() statements in build_request() if needed.
3

Verify Response Parsing

Check that assistant messages are being extracted correctly. Add print() in parse_response() to debug.
4

Scale Up Testing

Once basic tests work, increase complexity:

Best Practices

Copy one of the provided examples that most closely matches your API format, then modify incrementally.
Use print() liberally during development to see request/response structures.
Before writing your Rhai script, confirm you can successfully call your API with curl.
Add checks for missing or null fields in responses:
Focus on request transformation and response parsing. Complex logic should live in your API, not the script.

Real-World Use Cases

Internal Model Deployments

Test proprietary models deployed on internal infrastructure

Fine-Tuned Models

Evaluate custom fine-tunes on non-standard endpoints

Research Models

Test experimental models with unique API formats

Multi-Model Routing

Route requests to different models based on custom logic

Next Steps

Rhai Language Documentation

Complete reference for Rhai scripting language

Providers Overview

Learn about OpenAI and Ollama providers