highflame-shield Rust SDK
Async Rust client for the Highflame Shield guardrails service — the AI safety layer that detects threats and enforces Cedar policies on your LLM calls, tool executions, and model responses.
Contents
- Requirements
- Installation
- Authentication
- Quick Start
- Client API
- Agentic Context
- Error Handling
- Enforcement Modes
- Session Tracking
- Multi-Project Support
- Client Options
Requirements
- Rust 1.75+
- Tokio async runtime
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
For streaming, also add:
= "0.3"
Authentication
Create a client with your service key:
use ;
let client = new;
The API key can also be read from an environment variable:
let client = new;
For self-hosted deployments, override the service endpoints:
let client = new;
ShieldClient is cheap to clone — all clones share the same connection pool and token cache.
Quick Start
use ;
async
Client API
guard()
Full detection and Cedar policy evaluation. Accepts a ShieldRequest and returns a ShieldResponse.
use ;
let resp = client.guard.await?;
if resp.denied else if resp.alerted.unwrap_or else
ShieldRequest fields:
| Field | Type | Description |
|---|---|---|
content |
String |
Text to evaluate |
content_type |
String |
"prompt", "response", "tool_call", or "file" |
action |
String |
"process_prompt", "call_tool", "read_file", "write_file", or "connect_server" |
mode |
Option<String> |
"enforce" (default), "monitor", or "alert" |
session_id |
Option<String> |
Session ID for cross-turn tracking |
tool |
Option<ToolContext> |
Tool call context |
model |
Option<ModelContext> |
LLM metadata |
file |
Option<FileContext> |
File operation context |
mcp |
Option<McpContext> |
MCP server context |
ShieldResponse fields:
| Field | Type | Description |
|---|---|---|
decision |
String |
"allow" or "deny" |
actual_decision |
Option<String> |
Decision before mode override |
alerted |
Option<bool> |
True when an alert-mode policy fired |
reason |
Option<String> |
Human-readable explanation |
determining_policies |
Vec<DeterminingPolicy> |
Policies that drove the decision |
context |
HashMap<String, Value> |
Raw detector outputs |
projected_context |
HashMap<String, Value> |
Context sent to the policy evaluator |
session_delta |
Option<SessionDelta> |
Cross-turn state diff |
latency_ms |
Option<i64> |
Total request latency |
Helper methods on ShieldResponse:
resp.allowed // true when decision == "allow"
resp.denied // true when decision == "deny"
guard_prompt()
Shorthand for evaluating a user prompt.
// guard_prompt(content, mode, session_id)
let resp = client
.guard_prompt
.await?;
// Omit optional fields with None
let resp = client
.guard_prompt
.await?;
guard_tool_call()
Shorthand for evaluating a tool call by name and argument map.
use HashMap;
use json;
let mut args = new;
args.insert;
let resp = client
.guard_tool_call
.await?;
if resp.denied
Streaming
stream() returns an impl Stream<Item = Result<ShieldStreamEvent, ShieldError>>. Use pin_mut! before iterating.
use ;
use ;
let req = ShieldRequest ;
let stream = client.stream.await?;
pin_mut!;
while let Some = stream.next.await
ev.r#type |
Description |
|---|---|
"detection" |
A detector tier completed |
"decision" |
Final allow/deny decision |
"error" |
Stream error |
"done" |
Stream ended |
Agentic Context
Pass typed context structs to provide richer signal to detectors and Cedar policies.
ToolContext
use ;
use HashMap;
use json;
let resp = client.guard.await?;
| Field | Type | Description |
|---|---|---|
name |
String |
Tool name |
arguments |
Option<HashMap<String, Value>> |
Tool arguments |
server_id |
Option<String> |
MCP server that registered this tool |
is_builtin |
Option<bool> |
Whether the tool is a first-party built-in |
description |
Option<String> |
Tool description |
ModelContext
use ;
let resp = client.guard.await?;
McpContext and FileContext
use ;
// MCP server connection
let resp = client.guard.await?;
// File write
let resp = client.guard.await?;
Error Handling
All client methods return Result<_, ShieldError>. Match on the enum variants to handle specific cases:
use ShieldError;
match client.guard.await
| Variant | When returned | Fields |
|---|---|---|
ShieldError::Api |
Non-2xx HTTP response | status: u16, title: String, detail: String |
ShieldError::Connection |
Timeout or network failure | String message |
ShieldError::Deserialisation |
Response body could not be parsed | wraps serde_json::Error |
Helper methods:
err.status // Option<u16> — Some for Api variant, None otherwise
err.is_api_error // true for Api variant
err.is_connection_error // true for Connection variant
Unlike the Python and JavaScript SDKs, there is no
ShieldBlockedErrorin Rust. A deny decision is a successful response — inspectresp.denied()on the returnedShieldResponse.
Enforcement Modes
| Mode | Behavior | resp.denied() |
resp.alerted |
|---|---|---|---|
"enforce" |
Block on deny | true on deny |
None |
"monitor" |
Allow + log silently | false |
None |
"alert" |
Allow + trigger alerting pipeline | false |
Some(true) if violated |
// Monitor — observe without blocking
let resp = client.guard.await?;
if resp.actual_decision.as_deref == Some
// Alert — allow but signal the alerting pipeline
let resp = client.guard.await?;
if resp.alerted.unwrap_or
// Enforce — block violations (default)
let resp = client.guard.await?;
if resp.denied
Session Tracking
Pass the same session_id across all turns of a conversation to enable cumulative risk tracking. Shield maintains action history across turns, which Cedar policies can reference.
let session_id = format!;
let resp = client.guard.await?;
if let Some = &resp.session_delta
Multi-Project Support
Pass account_id and project_id to scope all requests to a specific project:
let client = new;
Client Options
ShieldClientOptions uses a builder pattern. All methods after new() are optional.
use Duration;
let opts = new
.base_url
.token_url
.timeout
.max_retries
.account_id
.project_id;
let client = new;
| Method | Default | Description |
|---|---|---|
new(api_key) |
— | Service key (hf_sk_...) or raw JWT |
.base_url(url) |
Highflame SaaS | Guard service URL |
.token_url(url) |
Highflame SaaS | Token exchange URL |
.timeout(duration) |
30s | Per-request timeout |
.max_retries(n) |
2 |
Retries on transient errors |
.account_id(id) |
— | Override tenant account ID |
.project_id(id) |
— | Override tenant project ID |