Skip to main content
HiddenLayer AI Runtime Security analyzes the prompts you send to a large language model (LLM) and the responses it returns. It looks for threats such as prompt injection, sensitive data leakage, and policy violations. Based on the policy configured for your project, HiddenLayer tells you whether to allow, redact, or block the traffic. The SDK gives you two families of endpoints for this:
  • Interactions (v1): client.interactions.analyze sends a prompt and an optional response in a simple message format and returns a detailed analysis.
  • Agentic evaluation endpoints (v2): client.runtime.evaluate_request, evaluate_response, and evaluate_interaction accept provider-native payloads (the exact JSON you send to or receive from OpenAI or Anthropic) and support agentic content such as tool calls.

Before you begin

  • Install the SDK and configure credentials. See Installation and Authentication and configuration.
  • Set HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET in your environment, or set HIDDENLAYER_TOKEN to a bearer token. Every example on this page reads credentials from the environment.
  • Your tenant needs an AI Runtime Security license. The v2 evaluation endpoints require Agentic Runtime Security access. See Get started with Agentic Runtime Security.
  • No extra Python packages are needed.
Authentication and evaluation are region-specific. If your account is in the EU, create the client with environment="prod-eu". The default is prod-us.

Choose an endpoint

The v2 endpoints auto-detect these provider formats: OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages. See Evaluation endpoints for details.
In SDK version 3.10.0, the three client.runtime.* methods emit a hiddenlayer.BetaWarning each time you call them. Beta endpoints have no compatibility guarantees and can change between SDK versions. See Beta warnings for how to filter the warning.

Analyze an interaction

Use client.interactions.analyze to analyze a prompt, a response, or both. It is the simplest way to get a detailed security analysis, and it is not a beta endpoint. Required and optional arguments:
  • metadata (required): model and requester_id are required. provider is optional. requester_id identifies the user or service that sent the prompt.
  • input: the prompt, as {"messages": [{"role": "user", "content": "..."}]}.
  • output: the model response, in the same format.
  • hl_project_id: the ID of a Console project. HiddenLayer applies that project’s policy. If you omit it, your tenant’s default project and its policy apply.
analyze_interaction.py
Expected result: the script prints the action, threat level, and the names of any detectors that fired. For the prompt in this example, expect a prompt injection detection. The exact action depends on your project’s policy.

Read the result

  • result.evaluation.action is Allow, Alert, Redact, or Block. Treat Block as “do not send or show this content”. evaluation is optional in the response type, so check for None before you read it.
  • result.evaluation.threat_level is None, Low, Medium, High, or Critical.
  • result.modified_data.input and result.modified_data.output contain the messages after any redaction. When the action is Redact, forward these instead of the original text.
  • result.analysis lists each detector that ran, with name, phase, detected, and findings.
  • result.metadata includes the event_id and the project that was applied.
For a full response sample, see Interactions.

Guard a request inline

Use evaluate_request to screen a prompt before your application sends it to the model. You pass the exact request payload you are about to send. HiddenLayer returns a payload in the same provider format:
  • If the policy allows or redacts, you get back the request (possibly redacted). Forward this returned payload to the model, not your original.
  • If the policy blocks, you get back a block message in the provider’s response format. Return it to the user and do not call the model.
The HTTP status is 200 in every case. A block is signaled only by the hl-runtime-action: BLOCK response header. The plain client.runtime.evaluate_request(...) method returns only the body, so use client.runtime.with_raw_response.evaluate_request(...) to read the header. Call .parse() on the raw response to get the body as a dict.
If you ignore the hl-runtime-action header and always forward the returned payload, blocked prompts are sent to your model as if they were allowed. Always check the header before you call the model.
The following example screens the request, calls the model, and then screens the response with evaluate_response, which works the same way. call_model is a placeholder that returns a fixed OpenAI Chat Completions response. Replace it with your real model call.
guard_inline.py
Expected result: for this harmless prompt, the script prints the model response payload unchanged. If your policy blocks a prompt, the script prints the block message instead, and call_model is never called.
This page does not describe the exact structure of the block message, because it follows the provider’s response format and may change. Treat it as an opaque provider response and pass it back to your caller. If you need to detect a block in later code, keep the header value instead of inspecting the body.
Decide in advance what your application does when HiddenLayer is unreachable. In the example, an APIConnectionError stops the request (fail closed). If you choose to forward the original request instead (fail open), unscreened prompts reach your model during an outage.

Evaluate an interaction with tool calls

Use evaluate_interaction when you need a structured verdict, when your traffic includes tool calls, or when you evaluate captured traffic after the fact, such as for monitoring or batch analysis. It returns a result object rather than a payload to forward. Arguments:
  • metadata (required): model, provider, and requester_id are required. external_session_id is optional and groups calls into a session.
  • interaction (required): either a provider-native payload, or an explicitly described interaction with messages and typed content parts. You can also include tools_available to describe the tools the agent can call (each with name, and optionally description and parameters).
  • hl_project_id (optional): apply a specific project’s policy.
The explicitly described format uses these content part types:
evaluate_interaction.py
Expected result: for this harmless exchange, the action is typically NONE, the threat level NONE, and the detections list is empty. The exact result depends on your project’s policy.

Read the outcome

  • outcome.action: NONE (no detections), DETECT (detections reported, payload unchanged), REDACT (payload modified), or BLOCK (a block response is substituted).
  • outcome.threat_level: NONE, LOW, MEDIUM, HIGH, or CRITICAL.
  • outcome.detections: a list of detections, each with rule_name and risk_level. The list is empty when no rules fired.
  • outcome.effective_interaction: the payload to forward. If you sent a provider-native payload, you get the same provider shape back as a dict.
  • result.evaluated_interaction.messages[].analysis.signals: per-message findings.
See Policy for how rules and actions are configured.

Group calls into a session

An agent often makes many model calls to complete one task. To review them together in the Console as one session, tag every call with the same session ID:
  • For evaluate_request and evaluate_response, pass hl_runtime_session_id=session_id.
  • For evaluate_interaction, set metadata["external_session_id"].
  • To pair a request with its response, pass the same HL-Roundtrip-Id header on both, using extra_headers={"HL-Roundtrip-Id": roundtrip_id}. There is no named argument for it.
Your application must generate the session ID once, when the workflow starts, and pass it on every call, including calls from tools and sub-agents. HiddenLayer cannot infer that separate calls belong together. See Agentic sessions for the full model.

Beta warnings

When you call a beta endpoint, the SDK emits a hiddenlayer.BetaWarning through Python’s standard warnings module. The message looks like this:
BetaWarning is a subclass of UserWarning. It does not stop your code. To hide it, add a filter once at startup:
The SDK changelog for version 3.9.0 says the beta label was removed from the v2 runtime endpoints, but version 3.10.0 still emits BetaWarning for evaluate_request, evaluate_response, and evaluate_interaction. The current status of these endpoints needs confirmation. See Version compatibility and upgrades and the Beta API FAQ.

Common errors and fixes

Check that HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET (or HIDDENLAYER_TOKEN) are set in the process that runs your code. Credentials are region-specific, so make sure the client’s environment (prod-us or prod-eu) matches your account. See Authentication and configuration.
Your tenant or API key may not have access to Runtime Security or Agentic Runtime Security. Contact your HiddenLayer account representative or Support.
The payload does not match what the endpoint expects. Common causes:
  • Sending an explicitly described interaction (typed content parts) to evaluate_request or evaluate_response. Those endpoints accept only provider-native payloads. Use evaluate_interaction instead.
  • Sending a response payload to evaluate_request, or a request payload to evaluate_response.
  • Missing required metadata keys, such as requester_id.
Print e.body from the exception for details from the API.
You are probably calling client.runtime.evaluate_request(...) directly, which returns only the body. Use client.runtime.with_raw_response.evaluate_request(...) and check the hl-runtime-action header, as shown in Guard a request inline.
evaluation is optional in InteractionAnalyzeResponse. Check for None before reading evaluation.action, and fall back to result.analysis if needed.
You passed environment while the HIDDENLAYER_BASE_URL environment variable is set. Unset the variable, or remove the environment argument.
Pass hl_project_id with the ID of the project whose policy you want. Without it, your tenant’s default project and its policy apply. Check result.metadata.project to see which project was used.
For more on exceptions, retries, and timeouts, see Error handling. For production guidance such as logging, see Security and production guidance.