Skip to content

Shields & empathy

Shields are classifiers that run before the main LLM call. Their output is available to the empathy engine, which can override or merge the response profile based on what the classifiers detect.

Every shield has an action that determines what happens when its threshold is crossed:

ActionBehavior
observeRecord the verdict and continue inference
flagRecord a warning but do not terminate inference
blockReject the request and return a refusal
end_callEnd a streaming audio call after playing the refusal, like block for voice channels

When action="block" triggers, inference terminates immediately. A shield.blocked.v1 event is recorded in the worklog, and the response is returned with status="error" because no LLM call ran. The configured persistence strategy still runs so the refusal is stored like any other turn.

Raw connector specs are also accepted and default to action="observe". For explicit control, wrap the connector in a shield-config. You can either embed the connector inline or reference a registered shield with shield_ref:

{
"shields": {
"toxicity": {
"provider_id": "shield-config",
"connector": {
"provider_id": "huggingface/text-classification",
"url": "https://your-hf-endpoint.com",
"token": { "$secretRef": "SHIELDS_PROVIDER_API_KEY" },
"label_map": { "LABEL_0": "safe", "LABEL_1": "toxic" }
},
"action": "block",
"threshold": 0.8,
"target_label": "toxic",
"fail_closed": true,
"block_message": "This request violates our safety policy."
}
}
}
FieldDefaultDescription
shield_ref—Reference to a ShieldRegistration by registered_shield_id; connector must be omitted
connector—The underlying classifier connector; required unless shield_ref is set
actionobserveobserve, flag, or block
threshold0.5Score at or above which flag/block triggers
target_label—Only this predicted label counts when set
fail_closedtrueIf true, reject the request when the shield errors or times out
block_message—Custom refusal text for blocked inputs or replaced results

By default a shield observes and fails closed. To take action on a classification, set action to block or flag explicitly.

Shield registrations let you define a connector once and reuse it across many agents. Register a shield with alquimia registry shields add <registered_shield_id> --connector '<json>', then reference it from any agent spec with shield_ref:

Terminal window
alquimia registry shields add toxicity-classifier \
--connector '{"provider_id":"huggingface/text-classification","url":"https://your-hf-endpoint.com","label_map":{"LABEL_0":"safe","LABEL_1":"toxic"}}'
{
"shields": {
"toxicity": {
"provider_id": "shield-config",
"shield_ref": "toxicity-classifier",
"action": "block",
"threshold": 0.8,
"target_label": "toxic",
"fail_closed": true
}
}
}

When shield_ref resolves, the registered connector is used as-is and the per-agent enforcement knobs (action, threshold, fail_closed, block_message) are still set inline. $secretRef markers inside the registered connector are resolved against the agentspace, so a shared shield can reference a shared secret without each agent spec embedding it.

A ShieldRegistration has the schema:

FieldDescription
registered_shield_idStable unique identifier for this shield
connectorReusable connector payload
nameDisplay name for client presentation
descriptionHuman-readable description
tagsFree-form labels for discovery and filtering

Content shields on tool and topic registrations

Section titled “Content shields on tool and topic registrations”

ToolRegistration and TopicRegistration can carry their own shields map. These content shields run against the result of a tool execution or knowledge retrieval before it is returned to the model context.

Topic shields are attached through the API with PUT /knowledge/topics/{topic_id}/shields. The runtime validates each shield against the registry at write time and stores the resulting config on TopicRegistration.shields. Tool shields are included in the tool registration payload and enforced on MCP tool results and A2A sub-agent results.

When a blocking content shield triggers on a tool output or retrieved chunk, the offending result is replaced with the shield’s block_message (or a default refusal) and a shield.blocked.v1 event is recorded. This lets prompt-injection patterns in retrieved documents or dangerous tool outputs be sanitized without aborting the whole turn.

When an out-of-process runtime worker evaluates a content shield over a server tool result, A2A result, or retrieved knowledge chunk, the worklog event includes a bounded, redacted snapshot of the surrounding conversation. The snapshot contains the user request, any prior tool results, and the value being shielded, but omits secrets and long context windows. This gives LLM-based content shields enough conversational grounding to detect policy violations that depend on earlier turns, while keeping the audit record compact and privacy-preserving.

{
"registered_tool_id": "my-mcp-server",
"name": "My MCP server",
"provider_id": "mcp",
"connection_config": { "url": "http://mcp-server" },
"shields": {
"prompt-injection": {
"provider_id": "shield-config",
"connector": { "provider_id": "alquimia/prompt-injection-detection" },
"action": "block",
"threshold": 0.5,
"block_message": "This tool result was removed by a safety policy."
}
}
}

alquimia/prompt-injection-detection is a built-in heuristic-only shield that requires no external endpoint. It uses regex rules to catch common prompt-injection patterns and is useful as a fast first-line filter, but it is not a guaranteed detector.

{
"shields": {
"prompt-injection": {
"provider_id": "shield-config",
"connector": {
"provider_id": "alquimia/prompt-injection-detection",
"heuristic_weight": 0.3
},
"action": "block",
"threshold": 0.5
}
}
}

LLM-based shields are full ResponseProfiles, so their config can use a registered model via model_ref.

Terminal window
cat > gpt-4o-mini-classifier.json <<'EOF'
{
"registered_model_id": "gpt-4o-mini-classifier",
"provider_id": "openai",
"name": "GPT-4o mini classifier",
"vendor": "openai",
"attribution_id": "openai/gpt-4o-mini",
"params": {
"model": "gpt-4o-mini",
"temperature": 0.0,
"api_key": {"$secretRef": "RESPONSE_PROVIDER_API_KEY"}
}
}
EOF
alquimia registry models add gpt-4o-mini-classifier.json --namespace default
{
"shields": {
"intent-classifier": {
"provider_id": "shield-config",
"connector": {
"provider_id": "alquimia",
"profile": {
"system_prompt": "Classify the user intent. Output JSON: {\"intent\": \"...\"}",
"evaluation_strategy": {
"evaluation_strategy_id": "one-shoot",
"structured_output": {
"method": "json_schema",
"json_schema": {
"type": "object",
"properties": { "intent": { "type": "string" } },
"required": ["intent"]
}
}
}
},
"config": { "model_ref": "gpt-4o-mini-classifier" }
},
"action": "observe"
}
}
}

Per-shield params override the registered model parameters.

The empathy engine evaluates rules against shield outputs and runtime context. The first matching rule overrides or merges the response profile.

{
"empathy": {
"rules": [
{
"rule_id": "toxic-input",
"strategy": "override",
"description": "Respond with a safety message when toxicity is detected",
"requirements": ["toxicity"],
"conditions": ["toxicity['label'] == 'toxic'"],
"response": {
"provider_id": "fixed",
"message": "I'm sorry, I can't help with that request."
}
},
{
"rule_id": "formal-tone",
"strategy": "merge",
"description": "Use formal tone for enterprise users",
"requirements": ["intent-classifier"],
"conditions": ["intent-classifier.get('intent') == 'enterprise'"],
"response": {
"provider_id": "alquimia",
"profile": {
"prompt_clauses": {
"tone": "Use formal, professional language. Avoid contractions."
}
}
}
}
]
}
}
StrategyBehavior
noneNo-op, useful as a placeholder
overrideReplace the entire response profile with rule.response
mergeDeep-merge rule.response into the main profile

conditions are Python expressions evaluated with simpleeval. Context keys are the shield IDs listed in requirements. A rule is only evaluated when all of its requirements are present.

  • Run cheap heuristic shields in block mode and expensive LLM shields in observe mode.
  • Set fail_closed=true for shields that guard sensitive content.
  • Use empathy merge for prompt-clause changes (language, tone, persona) and override for hard stops.
  • Attach content shields to tool/topic registrations when the risk is in the returned data, not the request.