Skip to main content

Create an API Key

  1. In your organization’s DeepRails API Console, go to API Keys.
  2. Click Create key, name it, then copy the key.
  3. (Optional) Save it as the DEEPRAILS_API_KEY environment variable.
API Keys – placeholder

Install the SDK

pip install deeprails

Create your first Defend workflow

Before you can submit events to be evaluated and potentially remediated, you have to create and configure a workflow.

A workflow is an abstraction for a specific production use of Gen AI, and its configurations determine which guardrail metrics are evaluated, at what thresholds, and how issues are remediated.

Types of Workflows

Workflows can either have custom or automatic thresholds, set by the threshold_type. Automatic workflows have adaptive thresholds that change as events are recorded, starting at low, medium, or high values in an automatic_hallucination_tolerance_levels dictionary. Custom workflows have static, fully customizable thresholds for each selected metric set as floating point values in a custom_hallucination_threshold_values dictionary.

Note that workflows set to automatic must have automatic_hallucination_tolerance_levels specified and custom_hallucination_threshold_values is not needed, and vice versa for those set to custom.
from deeprails import DeepRails

# Initialize (env var DEEPRAILS_API_KEY is recommended)
client = DeepRails(api_key="YOUR_API_KEY")

workflow_response = client.defend.create_workflow(
    name="Test Workflow",
    description="A workflow used to test the DeepRails API",
    threshold_type="custom",
    custom_hallucination_threshold_values={
        "completeness": 0.85,
        "instruction_adherence": 0.75,
    },
    improvement_action="fixit",
    max_improvement_attempts=5,
    web_search=True,
    file_search=["file_xxxxxxxxxxxx"],
    context_awareness=True,
)
print(workflow_response)

Required Parameters

FieldTypeDescription
namestringThe name of the workflow.
threshold_typestringThe workflow type (either automatic or custom), which determines whether thresholds are specified by the user or set automatically.
improvement_actionstringThe remediation strategy when outputs fail guardrail metrics. fixit rewrites the failing output to pass the metrics. regen prompts the LLM to regenerate the output from scratch. do_nothing records the failure without attempting remediation.

Optional Parameters

FieldTypeDescription
descriptionstringA description of the use case of the workflow or other additional information.
custom_hallucination_threshold_valuesobjectThe mapping of guardrail metrics to floating point threshold values (0.0-1.0). Required when threshold_type is custom. This determines which metrics Defend will evaluate and how strict each threshold is.
automatic_hallucination_tolerance_levelsobjectThe mapping of guardrail metrics to tolerance levels (low, medium, or high). Required when threshold_type is automatic. This determines which metrics Defend will evaluate and how strict the adaptive thresholds start.
max_improvement_attemptsintegerThe maximum number of improvement attempts to be applied to one workflow event before it is considered failed. Defaults to 10.
web_searchbooleanWhether or not the extended AI capability, web search, is available to the evaluation and remediation models.
file_searchstring[]A list of uploaded file IDs for the evaluation and remediation models to search using the extended AI capability, file search. Upload files first via /files/upload.
context_awarenessbooleanWhether or not the extended AI capability, context awareness, is available to the evaluation and remediation models.

Submit a Workflow Event

Use the SDK to log a production event (input + output). This creates a workflow event and automatically triggers an associated evaluation using the guardrail metrics you pass.

If the evaluation fails for one or more metrics, the improvement action specified for the affiliated workflow will be used to remediate the output. Then, that improved output will be evaluated and potentially improved again, if needed.

The improvement process will repeat for that event until all guardrails pass or the maximum number of retries is reached.
Tip: You can also submit a workflow event via the DeepRails API Playground.
from deeprails import DeepRails

# Initialize (env var DEEPRAILS_API_KEY is recommended)
client = DeepRails(api_key="YOUR_API_KEY")

event_response = client.defend.submit_event(
    workflow_id="wkfl_xxxxxxxxxxxx",
    model_input={
        "system_prompt": "You are a helpful tutor specializing in AP science classes.",
        "user_prompt": "Explain the difference between mitosis and meiosis in one sentence.",
        "ground_truth": "Mitosis produces two identical diploid daughter cells for growth and repair, while meiosis produces four genetically unique haploid gametes for sexual reproduction.",
        "context": [
            {"role": "user", "content": "I have an AP Bio exam tomorrow, can you help me study?"},
            {"role": "tutor", "content": "Sure, I'll help you study."}
        ]
    },
    model_output="Mitosis produces two genetically identical diploid cells for growth and tissue repair, whereas meiosis generates four genetically varied haploid gametes for sexual reproduction.",
    model_used="gpt-4o-mini",
    run_mode="precision",
    nametag="test",
)
print(event_response)

Required Parameters

FieldTypeDescription
workflow_idstringThe ID of the Defend workflow associated with this event. (find it in Console → Defend → Manage Workflows)
model_inputobjectYour prompt + optional context. Must include at least a user_prompt. See model_input fields below.
model_outputstringThe LLM output to be evaluated and recorded with the event.
model_usedstringThe model used to generate the output, like gpt-4o or o3.
run_modestringRun mode for the workflow event that determines which models are used to evaluate the event. Available run modes (fastest to most thorough): super_fast, fast, precision, precision_codex, precision_max, and precision_max_codex. Defaults to fast. Note: super_fast does not support Web Search or File Search — if your workflow has these enabled, use a different run mode or edit the workflow to disable them.

model_input Fields

FieldTypeRequiredDescription
user_promptstringYesThe user prompt sent to the LLM.
system_promptstringNoThe system prompt used to configure the LLM’s behavior.
ground_truthstringNoThe expected correct answer. Required when the workflow evaluates the ground_truth_adherence metric.
contextarrayNoStructured context for the evaluation, such as conversation history or domain-specific facts. Each item should have role and content fields. Required when context_awareness is enabled on the workflow.

Optional Parameters

FieldTypeDescription
nametagstringA user-defined tag for the event.

Retrieve Workflow and Event Details

You can retrieve workflow details and fetch events from a specific workflow. Events are processed asynchronously, so you will need to poll using the event ID until the status is Completed.
import time
from deeprails import DeepRails

# Initialize (env var DEEPRAILS_API_KEY is recommended)
client = DeepRails(api_key="YOUR_API_KEY")

WORKFLOW_ID = "wkfl_xxxxxxxxxxxx"
EVENT_ID = "evt_xxxxxxxxxxxx"

# Retrieve workflow details
workflow_response = client.defend.retrieve_workflow(
    workflow_id=WORKFLOW_ID
)
print(workflow_response)

# Poll event until completion
event_response = None
while True:
    event_response = client.defend.retrieve_event(
        workflow_id=WORKFLOW_ID,
        event_id=EVENT_ID,
    )
    if event_response.status == "Completed":
        break
    print(f"Event status: {event_response.status} — waiting...")
    time.sleep(2)

# Check the improvement outcome
tool_status = event_response.improvement_tool_status
if tool_status == "improved":
    print(f"Improved model output: {event_response.improved_model_output}")
elif tool_status == "improvement_failed":
    print("Improvement action failed to improve output")
elif tool_status == "no_improvement_required":
    print("Improvement not needed!")

Check Defend Outcomes via the API Console

  1. Open DeepRails API Console → Defend → Data.
  2. Filter by time range or search by workflow_id or nametag to find events.
  3. Open any event to see guardrail scores and remediation chains (FixIt/ReGen).
Defend data – placeholder

Next Steps

Configure Guardrails

Explore the metrics behind evaluations—correctness, safety, completeness, and more.

Defend Overview

Learn how workflows, metrics, and remediation work together.