Context Consistency (ctx_consist)
Contents
Metric Description
Context consistency is a trace-level metric: it measures whether each agent turn respected the constraints, facts, and earlier decisions it could see at that moment across a full OpenInference-format trace (an ordered list of spans). It scores snapshot fidelity — not whether claims were grounded in evidence the snapshot never contained, not whether the plan was a good one, and not whether the path was efficient.
The score runs from 0 (turns largely contradicted or ignored visible context) to 100 (every action respected the context).
Where the snapshot comes from
Knowledge is extracted once from the trace, then each LLM turn is judged only against the items visible to it. In case of conflicting facts/constraints between system and user and prompts, system prompt items are prioritized.
This metric does not score groundedness (a claim that no visible item supports or contradicts is not a violation), plan quality, plan adherence, path efficiency, whether the requested results were obtained, or whether the agent stopped at the right moment. Vague style instructions (for example “be concise”) are not penalized. Pair it with Plan Adherence when you care about following an intended procedure, with Plan Quality when you care about an optimal procedure given the task and tools, with Trajectory Efficiency when path cost matters, with Task Completion when you need whether the requested results were obtained, and with Termination Correctness when you need whether the agent stopped once the reachable work was settled.
How to interpret the score
- Closer to 100: scored turns largely respected the snapshot they could see; few or no constraint-violation rows.
- Closer to 0: many checks contradicted visible items, governing items were omitted, and/or constraint-violation rows are a large share of all checks.
A score that rounds to 100 can still list leftover violations in metadata (they did not move the rounded value). An empty violation list yields the explanation that every judged turn respected the context it could see.
Evaluation fails (score is null) when the payload is not a non-empty list of span objects, when the trace has no actionable steps, when no knowledge items can be built, when every judge batch fails, or when there were no applicable spans to be used for the evaluation.
There are no metric_args.
API usage
Prerequisites
After the environment variables are configured, the next step is to create a JSON payload. ctx_consist scores a whole trace. An inline trace is scored with POST /evaluate: send trace and omit span_id. A stored trace is scored with POST /runs/trace.
Shortname: ctx_consist
Default threshold: 80
Inputs (each object in data)
trace(list[object]required): An OpenInference-format trace — an ordered list of span dictionaries that share atrace_idand are linked byparent_id. The metric reads, among other fields:- Identity and nesting —
context.span_id,parent_id,openinference.span.kind. - LLM conversation and tool calls —
llm.input_messages.*(user/system prompts become constraints; assistant text becomes decisions),llm.output_messages.*.message.tool_calls.*(judged, not extracted). - Tool / retriever payloads —
tool.name,input.value/output.value, and related attributes (outputs become facts).
- Identity and nesting —
For annotated span/trace examples and the attribute map Aegis reads, see OpenInference spans and traces.
Evaluation metadata
On successful evaluation, the metric returns eval_metadata with:
violations(list[dict]): Kept defects — both check-level contradictions and omissions. Empty when every scored turn was clean.
Each item has:
unfollowed_source_idx(str): Span index of the knowledge item that was not respected (a step label such as"3", or a prompt batch such as"0/prompts").step_idx(str): Index of the span that is not following the context (for example"0/wave2").reason(str): Concise justification for the defect.
Example: inline trace
POST /evaluate. Send trace and omit span_id.
import json
import os
import requests
from dotenv import load_dotenv
load_dotenv(override=True)
_API_KEY = os.getenv("AEGIS_API_KEY")
_BASE_URL = os.getenv("AEGIS_API_BASE_URL")
_EVALUATE_URL = f"{_BASE_URL}/evaluate"
def post_evaluate(payload: dict) -> requests.Response:
"""POST JSON payload to single evaluation; returns the raw response."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {_API_KEY}",
}
return requests.post(
_EVALUATE_URL,
headers=headers,
data=json.dumps(payload),
)
if __name__ == "__main__":
# Minimal OpenInference-style trace (list of spans). Real traces include more.
trace = [
{
"name": "AgentRun",
"context": {
"trace_id": "0x3c25552ecfec75f0dfa5baad5a544c4c",
"span_id": "0x145e3c64b3b99a77",
},
"parent_id": None,
"start_time": "2026-07-08T11:07:57.720000Z",
"end_time": "2026-07-08T11:08:00.332983Z",
"status": {"status_code": "OK"},
"attributes": {"openinference.span.kind": "AGENT"},
},
{
"name": "Trip Planner",
"context": {
"trace_id": "0x3c25552ecfec75f0dfa5baad5a544c4c",
"span_id": "0x083b5fc1127d658a",
},
"parent_id": "0x145e3c64b3b99a77",
"start_time": "2026-07-08T11:07:57.725590Z",
"end_time": "2026-07-08T11:08:00.332983Z",
"status": {"status_code": "OK"},
"attributes": {"openinference.span.kind": "AGENT"},
},
{
"name": "response",
"context": {
"trace_id": "0x3c25552ecfec75f0dfa5baad5a544c4c",
"span_id": "0x64e5562a83068b87",
},
"parent_id": "0x083b5fc1127d658a",
"start_time": "2026-07-08T11:07:57.729073Z",
"end_time": "2026-07-08T11:07:59.573356Z",
"status": {"status_code": "OK"},
"attributes": {
"openinference.span.kind": "LLM",
"llm.input_messages.0.message.role": "system",
"llm.input_messages.0.message.content": (
"You plan trips. If any day is below 20°C, "
"repeat the process for Bangkok. Do not book anything."
),
"llm.input_messages.1.message.role": "user",
"llm.input_messages.1.message.content": (
"Plan a 4-day trip to Tokyo from New York, "
"departing 2026-10-12. Check the weather first."
),
"llm.tools.0.tool.json_schema": {
"type": "function",
"function": {
"name": "search_web",
"parameters": {"type": "object"},
},
},
"llm.output_messages.0.message.tool_calls.0.tool_call.id": (
"call_weather"
),
"llm.output_messages.0.message.tool_calls.0.tool_call.function.name": (
"search_web"
),
"llm.output_messages.0.message.tool_calls.0.tool_call.function.arguments": (
'{"query": "Tokyo weather 2026-10-13"}'
),
},
},
{
"name": "search_web",
"context": {
"trace_id": "0x3c25552ecfec75f0dfa5baad5a544c4c",
"span_id": "0x6c9868e6283f0ed1",
},
"parent_id": "0x083b5fc1127d658a",
"start_time": "2026-07-08T11:07:59.574350Z",
"end_time": "2026-07-08T11:07:59.589819Z",
"status": {"status_code": "OK"},
"attributes": {
"openinference.span.kind": "TOOL",
"tool.name": "search_web",
"tool.call.id": "call_weather",
"input.value": {"query": "Tokyo weather 2026-10-13"},
"output.value": "Tokyo forecast 2026-10-13: 6–9°C",
},
},
{
"name": "response",
"context": {
"trace_id": "0x3c25552ecfec75f0dfa5baad5a544c4c",
"span_id": "0xbbbbbbbbbbbbbbbb",
},
"parent_id": "0x083b5fc1127d658a",
"start_time": "2026-07-08T11:07:59.610000Z",
"end_time": "2026-07-08T11:08:00.300000Z",
"status": {"status_code": "OK"},
"attributes": {
"openinference.span.kind": "LLM",
"llm.input_messages.0.message.role": "system",
"llm.input_messages.0.message.content": (
"You plan trips. If any day is below 20°C, "
"repeat the process for Bangkok. Do not book anything."
),
"llm.input_messages.1.message.role": "user",
"llm.input_messages.1.message.content": (
"Plan a 4-day trip to Tokyo from New York, "
"departing 2026-10-12. Check the weather first."
),
"llm.input_messages.2.message.role": "assistant",
"llm.input_messages.2.message.tool_calls.0.tool_call.id": (
"call_weather"
),
"llm.input_messages.2.message.tool_calls.0.tool_call.function.name": (
"search_web"
),
"llm.input_messages.3.message.role": "tool",
"llm.input_messages.3.message.content": (
"Tokyo forecast 2026-10-13: 6–9°C"
),
"llm.output_messages.0.message.role": "assistant",
"llm.output_messages.0.message.contents.0.message_content.type": (
"text"
),
"llm.output_messages.0.message.contents.0.message_content.text": (
"Tokyo looks cold. I will continue with the Tokyo itinerary."
),
},
},
]
# Send trace and omit span_id. No metric_args.
payload = {
"metric_shortname": "ctx_consist",
"threshold": 80,
"model_slug": "o4-mini",
"trace": trace,
}
response = post_evaluate(payload)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))
Example: stored trace
POST /runs/trace. trace_id is the integer id of the stored trace. Omit spans.
import json
import os
import requests
from dotenv import load_dotenv
load_dotenv(override=True)
_API_KEY = os.getenv("AEGIS_API_KEY")
_BASE_URL = os.getenv("AEGIS_API_BASE_URL")
_TRACE_RUN_URL = f"{_BASE_URL}/runs/trace"
def post_trace_run(payload: dict) -> requests.Response:
"""POST JSON payload to a trace run; returns the raw response."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {_API_KEY}",
}
return requests.post(
_TRACE_RUN_URL,
headers=headers,
data=json.dumps(payload),
)
if __name__ == "__main__":
payload = {
"trace_id": 42,
"threshold": 80,
"model_slug": "o4-mini",
"is_blocking": True,
"selected_metrics": {
"ctx_consist": {
"threshold": 80,
},
},
}
response = post_trace_run(payload)
response.raise_for_status()
print(json.dumps(response.json(), indent=2))