ek.agents#
ek.agents – evaluating AI agents and assistants, in cost per successful task.
The agent instance of ek, mirroring ek.ocr. Agent evaluation is the same 2x2 the
framework already implements – (reference availability) x (granularity) – but the evaluated
object is an episode (tool calls + observations ending in a final state) and the objective is
cost per successfully completed task, not cost per token.
|---|—|---|
| reference-based, one item | grade an episode against a gold outcome/trajectory | ek.score() |
| reference-based, corpus | task-suite benchmark: pass^k + cost, per slice | run_suite() |
| reference-free, one item | judge / verifier / the cascade’s confidence gate | ek.estimate_quality() |
What it reuses (rather than rebuilds): the Layer-A GraphGrammar becomes the **task/tool
grammar** (FieldSpec.importance = the cost of a wrong argument, so a wrong argument to a
destructive tool is not one unit of error); FieldMetric’s TP/FP/FN accounting powers
tool-call scoring; the ROVER agreement seam is self-consistency; the IAA helpers validate a
judge; and the whole signal -> calibrate -> validate -> decide cascade is the confidence gate.
Quickstart:
>>> from ek.agents import TaskSpec, run_suite, as_agent
>>> tasks = [TaskSpec("t1", input="hi", gold="HI"), TaskSpec("t2", input="yo", gold="YO")]
>>> report = run_suite(as_agent(str.upper), tasks)
>>> report.success_rate
1.0
Research: misc/docs/ek_07 (map) - ek_08 (task success) - ek_09 (judge/QE) -
ek_10 (trajectory) - ek_11 (cost economics) - ek_12 (libraries + integration).
Functions
|
Build the Layer-A |
|
The union grammar over a whole task suite (later tools win on name collision). |
|
Coerce a |
|
Duck-typed episode check (core never imports this module -- it checks the shape). |
|
Assign predicted calls to gold calls -- the layer |
|
Canonicalized exact match on the final answer (the GAIA-style oracle). |
|
Compare the episode's final state with the goal state (the tau-bench oracle). |
|
Trust a |
|
Unbiased |
|
Unbiased |
|
Compute |
|
Wilson score interval for a proportion -- correct in the tails, unlike Wald/CLT. |
|
Percentile bootstrap CI for any |
|
Newcombe score interval for the difference of two proportions |
|
Upper bound of |
|
Standard error of a mean of |
|
Build a |
|
Monetary cost of one |
|
Monetary cost of one episode (0.0 if it carries no |
|
Expected monetary cost of one successful task: |
|
The quality x cost x latency triple, reported together. |
|
Parse a LiteLLM-format price catalog into a |
Path of the vendored LiteLLM price catalog shipped with ek (a stale-able snapshot). |
|
|
Resolve a model's rates from an injected catalog, or fail actionably. |
|
Certify a judge against human labels before trusting it. |
|
Pairwise preference for |
|
Run |
|
Freeze a run as a named baseline (including its provenance, so the gate can guard it). |
|
Load a named agent baseline (or |
|
Fail only when the evidence says the agent really got worse -- not on noise. |
|
Wrap a plain |
|
Parse a chat transcript into a |
|
Build a full |
|
Build a |
|
Adapt an Inspect AI |
|
Yield one graded |
|
An Inspect |
|
Run an Inspect AI eval and return its samples as graded |
|
Adapt a DeepEval |
Classes
|
One task in a suite: its input, its oracle, its value, and its allowed tools. |
|
One tool the agent may call: its argument schema and its error costs. |
|
Layer B: one agent run over one task -- the object agent evaluation scores. |
|
The ordered sequence of |
|
One step of a trajectory: a tool call and what came back. |
|
What one episode consumed: tokens (by kind), retries, and wall-clock. |
|
The hidden eval variables that decide whether two runs are even comparable. |
|
Did the episode complete the task? ( |
|
Cost-of-Pass: dollars per successfully completed task (lower is better). |
|
Tool-call correctness (BFCL AST match) as a cost-weighted |
|
Cost-weighted trajectory distance as a |
|
A reference-based LLM judge as a |
|
The success oracle: did this episode leave the world in the required state? |
|
Corpus-level reliability over a k-trial run, with per-task and per-slice cuts. |
|
Per-token rates for one model, in USD per token. |
|
Self-consistency confidence in |
|
A reference-free Ragas metric as a |
|
A DeepEval metric as an ek |
|
A reference-free, criteria-only LLM judge as a |
|
Outcome of a variance-aware agent regression check. |
Exceptions
Raised when a model's rates are not in the injected price catalog. |
- class ek.agents.AgentGateResult(passed=True, reasons=list(), pass_hat_k=None, pass_hat_k_baseline=None, success_rate=None, success_rate_baseline=None, cost_per_success=None, cost_per_success_baseline=None, underpowered=False)[source]#
Bases:
objectOutcome of a variance-aware agent regression check.
- class ek.agents.Checker(*args, **kwargs)[source]#
Bases:
ProtocolThe success oracle: did this episode leave the world in the required state?
Injected, never owned by
ek– a real suite plugs in a database-state comparison, a hidden-test runner, or a programmatic predicate. It must run isolated from the agent.
- class ek.agents.Cost(input_tokens=0, output_tokens=0, cached_input_tokens=0, reasoning_tokens=0, retries=0, latency_s=None)[source]#
Bases:
objectWhat one episode consumed: tokens (by kind), retries, and wall-clock.
Token kinds are tracked separately because they are priced asymmetrically – output typically costs ~5x input, cached input bills at a fraction, and reasoning tokens are billed. This is precisely why the objective is dollars per successful task and never a raw token count (
misc/docs/ek_11). Pricing lives inek.agents.cost; this dataclass is the provider-neutral tally.Example
>>> a = Cost(input_tokens=100, output_tokens=50) >>> b = Cost(input_tokens=10, output_tokens=5, retries=1) >>> c = a + b >>> c.input_tokens, c.output_tokens, c.retries (110, 55, 1)
- class ek.agents.CostPerSuccessMetric(check=None, *, price=None, prices=None)[source]#
Bases:
objectCost-of-Pass: dollars per successfully completed task (lower is better).
The per-episode
Score.valueis that episode’s dollar cost – the real number is the corpusaggregate,sum(dollars) / sum(successes), which isinfwhen nothing succeeded (infeasibility is the honest answer; a per-token metric reporting “$0.003/call” for an agent that never succeeds is actively misleading).- Parameters:
check (
Any) – The success oracle (as inTaskSuccessMetric).price (
Optional[ModelPrice]) – AModelPriceapplied to every episode, orNoneto resolve each episode’s model againstprices.prices (
Optional[Mapping[str,ModelPrice]]) – A{model: ModelPrice}catalog (seeek.agents.load_prices()).
- class ek.agents.DeepEvalMetric(kind='tool_correctness', *, metric=None, **metric_kwargs)[source]#
Bases:
objectA DeepEval metric as an ek
Metricover anEpisode.score(episode, gold)builds a DeepEvalLLMTestCasefrom the episode’s output and tool calls (goldis the expected output string, or a mapping withexpected_output/expected_tools) and returns DeepEval’s score as an ekScore.tool_correctnessis recall-flavored (divides bylen(expected_tools)), not F1: the label says so, anddetail['flavor']records it.- Parameters:
kind (
str) –tool_correctness|task_completion|geval.metric (
Any) – An injected DeepEval-shaped metric (.measure(case),.score);Nonebuilds the real one lazily (telemetry disabled first).**metric_kwargs – Passed to the DeepEval constructor (e.g.
criteria=for GEval, andmodel=– a model name or aDeepEvalBaseLLM– for any of them: DeepEval >= 4 builds its default OpenAI judge at construction, so withoutmodel=it needsOPENAI_API_KEYeven fortool_correctness). For GEval,namedefaults to"ek_geval"andevaluation_paramstoDEFAULT_GEVAL_PARAMS(strings are accepted).
- class ek.agents.Episode(task_id='', trajectory=Trajectory(), output=None, final_state=None, cost=None, success=None, run=None, provenance=None, meta=dict())[source]#
Bases:
objectLayer B: one agent run over one task – the object agent evaluation scores.
The direct analog of
AnnotatedExtraction: it rides alongside the frozen Layer-A grammar and carries the run’s metadata. It is scored offline against gold (ek.score()/ek.evaluate()) and estimated online against a judge or a consensus (ek.estimate_quality()) – the same object, both halves.- Parameters:
task_id (
str) – Which task this episode answers (the k-trial grouping key).trajectory (
Trajectory) – The tool calls taken.output (
Any) – The agent’s final user-facing answer.final_state (
Any) – The end state of the world (the DB, the filesystem) – what a state-based oracle actually grades. Success is a final-state check, not a surface-text check.success (
Optional[bool]) – Filled in by a checker;Noneuntil graded.run (
Optional[RunProvenance]) – TheRunProvenance(seed, model, simulator, suite version).provenance (
Optional[Provenance]) – Optional coreProvenancefor click-to-source audit.meta (
dict) – Anything else the bridge captured.
- class ek.agents.JudgeMetric(judge, criteria='')[source]#
Bases:
objectA reference-based LLM judge as a
Metric(thescore()side).A judge that reads the gold answer is not a reference-free signal, so it does not belong in
ek.estimate_quality(). It lives here.- Parameters:
- class ek.agents.JudgeSignal(judge, criteria='', cost_tier=5, name='judge')[source]#
Bases:
objectA reference-free, criteria-only LLM judge as a
Signal.- Parameters:
judge (
Callable) – An injected callable(output, *, criteria) -> float.ekcore never imports an LLM client; you pass your own (or anek[judge]adapter).criteria (
str) – The rubric the judge scores against, bound at construction (exactly asLogprobSignalbinds its aggregator), so the object still satisfiesSignal.__call__(output) -> float.cost_tier (
int) –JUDGE_COST_TIER– the most expensive tier.
- class ek.agents.ModelPrice(input=0.0, output=0.0, cached_input=None, reasoning=None)[source]#
Bases:
objectPer-token rates for one model, in USD per token.
- Parameters:
input (
float) – USD per input (prompt) token.output (
float) – USD per output (completion) token.cached_input (
Optional[float]) – USD per cached-input token; defaults toDEFAULT_CACHE_DISCOUNT * inputwhen not declared.reasoning (
Optional[float]) – USD per reasoning token; defaults to theoutputrate (reasoning tokens are billed as output by the major providers).
- class ek.agents.RagasSignal(metric=None, *, name='faithfulness', llm=None, cost_tier=3, **metric_kwargs)[source]#
Bases:
objectA reference-free Ragas metric as a
Signalover a RAG sample.The sample is a mapping/object with
user_input(orquestion),response(oranswer) andretrieved_contexts(orcontexts). The score is in[0, 1]. Ragas metrics call an LLM, so pass a configuredmetric(orllm=to build one).Both Ragas metric APIs are accepted: the collections API (
ascore(**fields)returning aMetricResult;ragas.metrics.collections, the one that survives Ragas 1.0) and the legacy one (single_turn_ascore(SingleTurnSample); deprecated since Ragas 0.4).- Parameters:
metric (
Any) – A Ragas metric object (either API), orNoneto buildnamefromllm.name (
str) –faithfulness|answer_relevancy|context_precision.llm (
Any) – The judge, used whenmetricisNone. AnInstructorBaseRagasLLM(fromragas.llms.llm_factory) builds the collections metric; any other Ragas LLM wrapper (e.g.LangchainLLMWrapper) builds the legacy one.cost_tier (
int) –3– an LLM call per sample.**metric_kwargs (
Any) – Passed to the Ragas constructor (e.g.embeddings=foranswer_relevancy).
Example
>>> class Fake: ... async def single_turn_ascore(self, sample): return 0.5 >>> RagasSignal(Fake())({"question": "q", "answer": "a", "contexts": ["c"]}) 0.5
- class ek.agents.ReliabilityReport(k=1, n_tasks=0, n_trials=0, n_success=0, success_rate=0.0, success_ci=(0.0, 1.0), pass_at_k=None, pass_hat_k=None, pass_hat_k_ci=(0.0, 1.0), pass_hat_k_sd=None, stochastic=True, per_task=dict(), per_slice=dict(), cost=dict(), detail=dict())[source]#
Bases:
objectCorpus-level reliability over a k-trial run, with per-task and per-slice cuts.
success_rateis micro (over all trials);pass_at_k/pass_hat_kare averaged over tasks (each task’s own unbiased estimator), never over trials.- pass_hat_k_se(*, z=1.96)[source]#
SE of
pass_hat_k:sd / sqrt(n_tasks)with the boundary floor (mean_standard_error());Noneifpass^kcould not be measured.
- class ek.agents.RunProvenance(seed=None, temperature=None, model='', simulator='', suite_version='', scaffold='')[source]#
Bases:
objectThe hidden eval variables that decide whether two runs are even comparable.
Agent evaluation is stochastic, and several of its inputs are themselves models. In a tau-bench-style suite the user simulator is an LLM agent, so its model/prompt/version silently changes results run to run. Recording these is what lets the harness refuse to compare a baseline against a run that changed the simulator or the suite – the same discipline
ek’s offline harness already applies when it refuses to compare across two different metrics (misc/docs/ek_08,misc/docs/ek_11).- Parameters:
seed (
Optional[int]) – RNG seed for the trial (None= unseeded/nondeterministic).temperature (
Optional[float]) – Sampling temperature of the agent under test.model (
str) – The agent’s model id.simulator (
str) – The user-simulator id/version – an eval variable, not a constant.suite_version (
str) – Version of the task suite (contamination and task fixes move scores).scaffold (
str) – The agent scaffold/harness id (the same model moves points on scaffold alone).
- class ek.agents.Step(tool, args=dict(), observation=None, error=None)[source]#
Bases:
objectOne step of a trajectory: a tool call and what came back.
erroris set when the call itself failed (a bad argument, a tool exception) – error recovery across subsequent steps is itself an evaluable signal.
- class ek.agents.TaskSpec(task_id, input=None, gold=None, value=1.0, tools=(), slice=None)[source]#
Bases:
objectOne task in a suite: its input, its oracle, its value, and its allowed tools.
- Parameters:
task_id (
str) – Stable identifier (the grouping key for k-trial reliability).input (
Any) – Whatever the agent under test is called with.gold (
Any) – The reference the checker grades against (a goal state, an expected answer, a gold trajectory – the checker decides how to read it).value (
float) – Task-level cost weight: how much a completed task is worth. The task-level analog ofFieldSpec.importance; feeds value-weighted cost accounting.tools (
Sequence[ToolSpec]) – The tools this task permits (the Layer-A grammar for the task).slice (
Optional[str]) – Optional slice label (domain, difficulty, language) – per-slice cuts are mandatory, not optional (a low pass^k concentrated in one hard slice is a different problem from uniform flakiness).
- class ek.agents.TaskSuccessMetric(check=None)[source]#
Bases:
objectDid the episode complete the task? (
1.0/0.0; aggregate = success rate).
- class ek.agents.ToolCallMetric(level='call', *, canonicalizer=None, grammar=None)[source]#
Bases:
objectTool-call correctness (BFCL AST match) as a cost-weighted
Metric.- Parameters:
level (
str) –"call"(default) scores whole calls – a call is a TP only on an exact AST match;"arg"gives partial credit at argument granularity.canonicalizer –
str -> strapplied to string arguments before comparison (BFCL’s case/whitespace folding). Defaults toek’s default canonicalizer.grammar (
Optional[GraphGrammar]) – Layer-A grammar supplying per-tool and per-argument cost weights (may also be passed per call).
The
Scorecarriestp/fp/fnindetailsoek.evaluate()aggregates a correct micro-averaged F1 over the corpus rather than averaging per-episode F1s.
- class ek.agents.ToolSpec(name, params=dict(), importance=None, destructive=False)[source]#
Bases:
objectOne tool the agent may call: its argument schema and its error costs.
- Parameters:
name (
str) – The tool/function name (the AST-match ground truth).params (
Mapping[str,FieldSpec]) – Argument name ->FieldSpec. Each spec’simportanceis the cost of getting that argument wrong, and itsdomainfeeds validators – the same contract as an extracted field.importance (
Optional[float]) – Cost weight of the call itself (a missing or spurious call). Defaults toDESTRUCTIVE_WEIGHTwhendestructiveis set, else 1.0.destructive (
bool) – Whether calling this tool mutates the world irreversibly (a refund, a delete, a send). Drives the default weight and the safety validators.
- class ek.agents.Trajectory(steps=())[source]#
Bases:
objectThe ordered sequence of
Steps an agent took.A trajectory is linear – which is why it is scored with a sequence edit distance, not a graph edit distance (see
ek.agents.trajectory).Example
>>> t = Trajectory([Step("search", {"q": "cat"}), Step("answer", {"a": "meow"})]) >>> len(t), t.tools (2, ('search', 'answer'))
- class ek.agents.TrajectoryMetric(scheme='in_order', *, grammar=None, canonicalizer=None)[source]#
Bases:
objectCost-weighted trajectory distance as a
Metric(lower is better).- Parameters:
scheme (
str) – One ofSCHEMES.in_order(default) – Needleman-Wunsch edit distance; order matters, gaps allowed.exact– 0 iff the call sequences are identical, else the full mass.any_order– multiset distance; order ignored (the calls, not the path).superset– 0 iff every gold call occurs in the prediction; extra steps are free (use when the reference lists required calls rather than the whole path).grammar (
Optional[GraphGrammar]) – Layer-A grammar supplying step/argument cost weights (may be passed per call).canonicalizer –
str -> strapplied to string arguments before comparison.
- exception ek.agents.UnknownModelPrice[source]#
Bases:
KeyErrorRaised when a model’s rates are not in the injected price catalog.
- class ek.agents.UqlmConsistencySignal(scorer=None, *, llm=None, scorer_name='noncontradiction', num_responses=5, cost_tier=3)[source]#
Bases:
objectSelf-consistency confidence in
[0, 1]fromuqlm’s black-box scorers.Samples
num_responsesgenerations for the prompt and measures how much they agree (non-contradiction, exact match, semantic negentropy, …). Raw and uncalibrated: calibrate before gating (Hard Rule 1).- Parameters:
scorer (
Any) – A backend withscore(prompt) -> float(tests / custom sampling), orNoneto build uqlm’sBlackBoxUQfromllm.llm (
Any) – A LangChain chat model, required whenscorerisNone. uqlm reads itstemperatureattribute, so it must have one.scorer_name (
str) – The uqlm scorer column to read (default"noncontradiction", which downloads an NLI model on first use;"exact_match"needs none).num_responses (
int) – Samples per prompt.cost_tier (
int) –3– it pays fornum_responsesextra generations.
Example
>>> class Fake: ... def score(self, prompt): return 2.0 # out-of-range is clamped >>> UqlmConsistencySignal(scorer=Fake())("q") 1.0
- ek.agents.agent_regression_gate(report, baseline, *, tolerance=0.0, cost_tolerance=0.0, rootdir=None)[source]#
Fail only when the evidence says the agent really got worse – not on noise.
Tests the difference
current - baseline, with both runs’ uncertainty folded in: a Newcombe score interval on the success rate, and (fork > 1) a combined-SE bound onpass^k. A regression is declared only when the upper bound of that change is below-tolerance– i.e. we are confident the true change is negative.Two tempting shortcuts are both wrong, and this module has shipped and fixed each:
Comparing our interval to the baseline’s point. The baseline is itself a noisy estimate; a Wilson upper bound only reaches 1.0 when every trial passed, so a single flake against a lucky baseline reads as a “confident regression”.
Asking whether the two 95% intervals overlap. That is not a 5% test but roughly a 0.5% one: it buys a calm gate by going blind (a 15-point drop on a 60-task suite becomes invisible). A gate that cannot see a real regression is as broken as one that cries wolf.
A result that is merely worse but not significantly so is not a clean pass either – it is an underpowered experiment, and the result says so (
underpowered, plus a warning).- Raises:
ValueError – if the baseline was produced with a different user simulator, suite version, or scaffold – those runs are not comparable, and silently comparing them would be a category error (the same reason the offline gate refuses to compare two different metrics); if the baseline recorded zero trials (a permanent free pass); or if it is malformed.
- Return type:
- ek.agents.as_agent(fn)[source]#
Wrap a plain
input -> answerfunction into the harness’sTaskSpec -> Episodeshape.Progressive disclosure: the trivial agent should not have to learn the Episode type.
Example
>>> from ek.agents.base import TaskSpec >>> agent = as_agent(str.upper) >>> agent(TaskSpec("t1", input="hi")).output 'HI'
- Return type:
- ek.agents.as_call(x)[source]#
Coerce a
Step,(tool, args)tuple, or{"tool":…, "args":…}dict.Gold tool calls are usually written as bare pairs or dicts (no observation), while a prediction arrives as a
Step; both must compare. Progressive disclosure: the simple literal works, the rich object works.Example
>>> as_call(Step("search", {"q": "cat"})) ('search', {'q': 'cat'}) >>> as_call(("search", {"q": "cat"})) ('search', {'q': 'cat'}) >>> as_call({"tool": "search", "args": {"q": "cat"}}) ('search', {'q': 'cat'}) >>> as_call("search") ('search', {})
- Return type:
- ek.agents.bootstrap_ci(values, statistic, *, resamples=2000, alpha=0.05, seed=0)[source]#
Percentile bootstrap CI for any
statisticovervalues(deterministic by seed).Used for the cost-per-success ratio, whose distribution is skewed and heavy-tailed (a normal interval on it is meaningless).
Example
>>> lo, hi = bootstrap_ci([1.0] * 20, lambda v: sum(v) / len(v), resamples=50) >>> lo == hi == 1.0 True
- Return type:
- ek.agents.bundled_prices_path()[source]#
Path of the vendored LiteLLM price catalog shipped with ek (a stale-able snapshot).
- Return type:
- ek.agents.cost_from_usage(usage, *, latency_s=None)[source]#
Build a
Costfrom a providerusageobject or dict.Understands the OpenAI (
prompt_tokens/completion_tokens) and Anthropic (input_tokens/output_tokens/cache_read_input_tokens) spellings.Example
>>> c = cost_from_usage({"prompt_tokens": 100, "completion_tokens": 20}) >>> c.input_tokens, c.output_tokens (100, 20)
- Return type:
- ek.agents.cost_of_pass(total_cost, n_success)[source]#
Expected monetary cost of one successful task:
total_cost / n_success.Returns
infwhen nothing succeeded – infeasibility is the honest answer, not a cheap-looking zero (this divergence is the point of the metric).Example
>>> cost_of_pass(10.0, 4) 2.5 >>> cost_of_pass(10.0, 0) inf
- Return type:
- ek.agents.cost_report(episodes, *, prices=None, price=None)[source]#
The quality x cost x latency triple, reported together.
Scoring accuracy without cost lets an agent chase tiny gains with unbounded API calls, so a report that omits cost is not a report. Returns
n,n_success,success_rate,total_dollars,cost_per_success(Cost-of-Pass),mean_latency_sandtotal_tokens.With no
price/prices, dollar figures areNonerather than a fabricated zero:ekwill not invent rates, and a silent 0.0 would make a costly agent look free. Tokens and latency are still reported.Example
>>> from ek.agents.base import Cost, Episode >>> p = per_million(1.0, 1.0) >>> eps = [Episode(task_id="a", cost=Cost(input_tokens=1_000_000), success=True), ... Episode(task_id="b", cost=Cost(input_tokens=1_000_000), success=False)] >>> r = cost_report(eps, price=p) >>> r["success_rate"], round(r["cost_per_success"], 2) (0.5, 2.0) >>> cost_report(eps)["cost_per_success"] is None # no rates -> no dollars invented True
- Return type:
- ek.agents.difference_upper_bound(value_a, ci_a, value_b, ci_b, *, z=1.96)[source]#
Upper bound of
a - bfor two statistics known only through their CIs.Used for
pass^k(a mean of per-task estimators, not a raw proportion), where a Newcombe interval does not apply but the same principle does: test the difference, combining both runs’ uncertainty, rather than asking whether two intervals happen to overlap.Back-deriving an SE from a CI is the fallback: it under-estimates near p = 1 (a truncated percentile interval) and reads a zero-width CI as certainty. When the per-task SD is known, prefer
mean_standard_error()(the regression gate does, for any summary that persistspass_hat_k_sd).
- ek.agents.dollars(cost, price, *, prices=None, batch=False)[source]#
Monetary cost of one
Costtally, in USD.- Parameters:
cost (
Cost) – The token/retry tally.price (
Union[ModelPrice,str]) – AModelPrice, or a model name to resolve againstprices.prices (
Optional[Mapping[str,ModelPrice]]) – The injected price catalog (required whenpriceis a name).batch (
bool) – Apply the batch-API discount (DEFAULT_BATCH_DISCOUNT).
- Raises:
UnknownModelPrice – if a model name is given with no rates for it.
- Return type:
- ek.agents.episode_dollars(episode, *, prices=None, price=None, batch=False)[source]#
Monetary cost of one episode (0.0 if it carries no
Cost).- Return type:
- ek.agents.episode_from_messages(messages, *, task_id='', usage=None, output=None, final_state=None, latency_s=None)[source]#
Build a full
Episodefrom a transcript (+ optional usage/state).outputdefaults to the last assistant text – the agent’s final answer.- Return type:
- ek.agents.episodes_from_inspect_log(log, *, scorer=None, success_threshold=1.0, task_prefix='')[source]#
Yield one graded
Episodeper sample per epoch of an InspectEvalLog.task_prefix(e.g. the Inspect task name) is prepended as"<prefix>/<sample id>"– needed when pooling several logs, whose sample ids (often1, 2, 3) would otherwise collide and be counted as extra trials of one task. Ascorerthat no sample has raisesValueError(a typo would otherwise leave every episode silently ungraded).Duck-typed like
from_inspect_sample()(noinspect_aiimport): readslog.samplesandlog.eval. Each sample’s score – thescorernamed, else the first – is read withinspect_score_value()and the episode is gradedsuccess = value >= success_threshold; a sample with no scalar score stays ungraded (success=None) rather than being silently counted as a failure.Inspect’s
epochsare ek’s trials: an eval run withepochs=kyields k episodes pertask_id(the sample id), which is exactly whatreliability()needs forpass^k. The run’s model and task version are recorded asRunProvenance.Example (a log-shaped stand-in; a real
EvalLoghas the same attributes):>>> from types import SimpleNamespace as NS >>> sample = NS(id="q1", epoch=1, messages=[], output=NS(completion="4", usage=None), ... scores={"match": NS(value="C")}) >>> log = NS(samples=[sample], eval=NS(model="mockllm/model", task_version=0)) >>> [(e.task_id, e.success, e.meta["epoch"]) for e in episodes_from_inspect_log(log)] [('q1', True, 1)]
- ek.agents.final_state_match(episode, gold)[source]#
Compare the episode’s final state with the goal state (the tau-bench oracle).
- Return type:
- ek.agents.from_deepeval_test_case(case, *, task_id='')[source]#
Adapt a DeepEval
LLMTestCase-shaped object into anEpisode(duck-typed).Reads
.actual_outputand.tools_called. Note DeepEval phones home by default (Confident-AI telemetry) – disable it before use if that matters to you;eknever enables it.- Return type:
- ek.agents.from_inspect_sample(sample, *, task_id='')[source]#
Adapt an Inspect AI
EvalSample-shaped object into anEpisode(duck-typed).Reads
.messages,.outputand.id– noinspect_aiimport required, so scoring an Inspect log never drags the harness intoek’s dependency closure.- Return type:
- ek.agents.inspect_score_value(value)[source]#
An Inspect
Score.valueas a float, orNonewhen it is not a scalar grade.Example
>>> inspect_score_value("C"), inspect_score_value("P"), inspect_score_value(0.25) (1.0, 0.5, 0.25) >>> inspect_score_value({"accuracy": 1.0}) is None # a multi-value score: no scalar True
- ek.agents.is_episode(x)[source]#
Duck-typed episode check (core never imports this module – it checks the shape).
- Return type:
- ek.agents.judge_validation(judge_labels, human_labels, *, level='nominal')[source]#
Certify a judge against human labels before trusting it. No new code – reuse the IAA.
A judge is just another annotator, so
ek’s existing inter-annotator-agreement machinery (which already tells you the ceiling of a gold standard) is exactly the right instrument: Krippendorff’s alpha is the default (it alone handles any number of raters, any measurement level, and missing data); Cohen’s kappa applies for exactly two raters on complete nominal data. This reproduces the MT-Bench judge-vs-human protocol.- Return type:
- Returns:
alpha,kappa,percent_agreement,n, and averdictagainst the conventional thresholds (alpha >= 0.8 good, 0.67-0.8 tentative, below that unreliable).
Example
>>> r = judge_validation([1, 1, 0, 0], [1, 1, 0, 0]) >>> r["alpha"], r["verdict"] (1.0, 'good')
- ek.agents.load_agent_baseline(name, *, rootdir=None)[source]#
Load a named agent baseline (or
None).
- ek.agents.load_prices(source=None)[source]#
Parse a LiteLLM-format price catalog into a
{model: ModelPrice}table.sourceis a path to (or an already-parsed mapping of) LiteLLM’s MIT-licensedmodel_prices_and_context_window.json;None(default) uses the bundled snapshot (bundled_prices_path()). We read the data file only and never import thelitellmSDK – itsenterprise/subtree is a proprietary carve-out, and the SDK drags a large transitive tree in just to look up a rate.Entries lacking token rates (embeddings, unpriced models) are skipped.
Example
>>> table = load_prices({"m": {"input_cost_per_token": 1e-6, ... "output_cost_per_token": 2e-6}}) >>> table["m"].output 2e-06
- Return type:
- ek.agents.match_calls(pred, gold, *, canon=None)[source]#
Assign predicted calls to gold calls – the layer
FieldMetricdoes not provide.Calls are grouped by tool name (a call to the wrong tool is never a match). Within a tool name, candidate pairs are scored and taken globally best-first (exact-argument matches before partial ones, more agreeing arguments before fewer), so identical repeated calls are interchangeable and a reordered pair is still matched – while a strong pairing can never be stolen by a weaker one that merely happened to come first in the prediction.
This is a deterministic greedy approximation to the optimal assignment. It is exact for the common cases (distinct calls, repeats, reorderings); a full min-cost bipartite matching (Hungarian) could differ only on pathological many-to-many partial-overlap sets, and is not worth a dependency here.
Returns
(pairs, unmatched_pred, unmatched_gold),pairsbeing(pred_call, gold_call).Example
>>> pairs, up, ug = match_calls([("s", {"q": "b"}), ("s", {"q": "a"})], ... [("s", {"q": "a"}), ("s", {"q": "b"})]) >>> len(pairs), up, ug # order-insensitive within a tool name (2, [], [])
- Return type:
- ek.agents.mean_standard_error(mean, sd, n, *, z=1.96, bounds=(0.0, 1.0))[source]#
Standard error of a mean of
nper-unit values inbounds, floored near the bounds.The plain
sd / sqrt(n)is the right SE for a mean of independent per-task estimators (pass^kis exactly that) – but it collapses to zero when every task scored the same, which is common at the boundary (a perfect baseline: every task passed every trial). A zero SE claims certainty and turns a two-sample test back into interval-vs-point.The floor is Agresti–Coull’s trick generalised to bounded values: add
z**2 / 2pseudo-units at each bound, and take the SE of the augmented mean. For 0/1 values this is (up to the sample-variancen - 1) the Agresti–Coull SEsqrt(p~(1-p~)/(n+z**2)). With well-spread per-task values it is within a few percent ofsd / sqrt(n); it binds where the spread is small – near the bounds, or when every task scores about the same – which is conservative there. The result ismax(plain, floor). It is a normal (z) SE with no small-sample t-correction, so with fewer than ~20 tasks it is somewhat optimistic.Returns
Nonewhen there is nothing to estimate from (n <= 0or nomean). A missingsd(fewer than two units) contributes nothing; the floor still applies.Example
>>> round(mean_standard_error(0.5, 0.3, 100), 4) # interior: ~ sd/sqrt(n) = 0.03 0.0304 >>> mean_standard_error(1.0, 0.0, 60) > 0 # perfect: never zero True >>> round(mean_standard_error(1.0, 0.0, 60), 4) # ~ Agresti-Coull for 60/60 0.0215
- ek.agents.newcombe_difference(successes_a, n_a, successes_b, n_b, *, z=1.96)[source]#
Newcombe score interval for the difference of two proportions
p_a - p_b.This is the right instrument for “did the agent get worse than its baseline?”, and it is strictly better than the tempting shortcut of checking whether two 95% CIs overlap. Non-overlap is not a 5% test – it is roughly a 0.5% one, so it buys its low false-alarm rate by going nearly blind: on a 60-task suite it cannot see a 15-point success-rate drop. A difference interval keeps the same false-alarm rate and recovers most of the power, because the variance of a difference is not the sum of two interval widths.
Newcombe (1998) method 10: build each proportion’s Wilson interval, then
lower = (p_a - p_b) - sqrt((p_a - l_a)^2 + (u_b - p_b)^2) upper = (p_a - p_b) + sqrt((u_a - p_a)^2 + (p_b - l_b)^2)
Returns
(lower, upper). The difference is a confident regression whenupper < 0.Example
>>> lo, hi = newcombe_difference(50, 100, 90, 100) # 50% now vs 90% before >>> hi < 0 # confidently worse True >>> lo, hi = newcombe_difference(89, 100, 90, 100) # 89% vs 90% -- noise >>> hi > 0 True
- Return type:
- ek.agents.output_match(episode, gold)[source]#
Canonicalized exact match on the final answer (the GAIA-style oracle).
The weakest useful oracle, and the default only because it needs nothing. Prefer a state-based or executable checker whenever the task has one.
- Return type:
- ek.agents.pairwise_judge(judge, a, b, *, criteria='', swap=True)[source]#
Pairwise preference for
aoverb, corrected for position bias.Position bias is not a rounding error – judges materially prefer whichever answer they see first. The standard correction is to ask twice with the order swapped and average, which is why this cannot be a plain
Signal(it needs two outputs).- Parameters:
- Return type:
- Returns:
Preference for
ain[0, 1](0.5= a tie).
Example
>>> first_wins = lambda x, y, *, criteria="": 1.0 # a maximally position-biased judge >>> pairwise_judge(first_wins, "A", "B") # swapping exposes it as a tie 0.5
- ek.agents.pass_at_k(*, n, c, k)[source]#
Unbiased
pass@k: probability that at least one of k samples succeeds.The HumanEval estimator
1 - C(n-c, k) / C(n, k)overntrials withcsuccesses. This is the capability metric – right when a single success is enough (offline candidate generation behind a verifier).Example
>>> round(pass_at_k(n=10, c=1, k=1), 3) 0.1 >>> pass_at_k(n=10, c=10, k=5) 1.0
- Return type:
- ek.agents.pass_hat_k(*, n, c, k)[source]#
Unbiased
pass^k: probability that all k independent trials succeed.The tau-bench estimator
C(c, k) / C(n, k); for a per-trial success probabilitypit decays top**k. This is the reliability metric – the one that matters when consistency is the product.Example
>>> pass_hat_k(n=8, c=8, k=8) 1.0 >>> pass_hat_k(n=8, c=4, k=2) 0.21428571428571427
- Return type:
- ek.agents.per_million(input, output, *, cached_input=None, reasoning=None)[source]#
Build a
ModelPricefrom the human-facing USD per 1M tokens figures.Example
>>> p = per_million(3.0, 15.0) # $3 in / $15 out per 1M tokens >>> round(p.input, 9), round(p.output, 9) (3e-06, 1.5e-05)
- Return type:
- ek.agents.price_of(model, *, prices=None)[source]#
Resolve a model’s rates from an injected catalog, or fail actionably.
eknever guesses a rate (they go stale monthly). Supply a catalog withprices=;load_prices()loads the bundled LiteLLM snapshot or your own file.- Return type:
- ek.agents.recorded_success(episode, gold)[source]#
Trust a
successalready recorded on the episode (e.g. by an external harness).- Return type:
- ek.agents.reliability(source, *, k=1, slices=None, cost=None, warn_deterministic=True)[source]#
Compute
pass@k/pass^k/ success-rate over a k-trial run.- Parameters:
source (
Any) – GradedEpisodes (grouped bytask_id), or an already-grouped{task_id: [bool, ...]}mapping.k (
int) – The k of pass@k / pass^k. Tasks with fewer than k trials are skipped (and counted indetail['skipped']) rather than silently mis-estimated.slices (
Optional[Mapping[str,str]]) – Optional{task_id: slice_label}for per-slice cuts.cost (
Optional[Mapping]) – An optional cost report (fromek.agents.cost_report()) to carry along – reliability without cost is only half the picture.warn_deterministic (
bool) – Warn when no task shows mixed outcomes across trials, in which casepass^kdegenerates topass^1and is not informative.
- Return type:
Example
>>> r = reliability({"t1": [True, True, False], "t2": [True, True, True]}, k=2) >>> r.n_tasks, r.k (2, 2) >>> round(r.pass_hat_k, 3) # t1: C(2,2)/C(3,2)=1/3 ; t2: 1.0 0.667
- ek.agents.run_inspect(tasks, *, model=None, epochs=None, scorer=None, success_threshold=1.0, log_dir=None, **eval_kwargs)[source]#
Run an Inspect AI eval and return its samples as graded
Episodes.A thin convenience over
inspect_ai.eval+episodes_from_inspect_log(): one episode per sample per epoch, graded by the sample’s score (scorer, else the first) reachingsuccess_threshold. Run withepochs=kand the result is a k-trial run – hand it toreliability()forpass^k, or tocost_report()for cost per successful task.- Parameters:
tasks (
Any) – Anythinginspect_ai.evalaccepts (aTask, a task function, a registry name, a list of them).model (
Any) – The model under test (e.g."openai/gpt-4o");Nonelets Inspect useINSPECT_EVAL_MODEL."mockllm/model"runs offline at no cost.epochs (
Optional[int]) – Trials per sample (Inspect’sepochs);Nonekeeps the task’s own setting.scorer (
Optional[str]) – Which scorer’s value grades success (default: each sample’s first score).success_threshold (
float) – A sample succeeds when its score value is>=this.log_dir (
Optional[str]) – Where Inspect writes its.evallogs.Noneuses a temporary directory that is removed once the episodes are read – pass a path to keep the logs.**eval_kwargs (
Any) – Passed toinspect_ai.eval(limit=,solver=, …).displaydefaults to"none".
- Raises:
RuntimeError – if any eval did not finish with status
success(a partial run is not a k-trial run, and grading it as one would understate failures).- Return type:
Note
inspect_ai.evalis synchronous and refuses to run inside an event loop; from async code, callinspect_ai.eval_asyncand pass each log toepisodes_from_inspect_log.
- ek.agents.run_suite(agent, tasks, *, k=1, check=None, metrics=None, price=None, prices=None, run=None, seed=None, persist=False, run_id=None, rootdir=None)[source]#
Run
agentover a task suite,ktrials per task, and report reliability + cost.- Parameters:
agent (
Callable[[TaskSpec],Any]) – The system under test:TaskSpec -> Episode(a bare answer is wrapped). It receives the whole spec –.input,.tools,.gold– not just the input. If it accepts a keywordseed, the per-trial seed is passed to it.k (
int) – Trials per task.k > 1is what makespass^kmeaningful – and it only means anything if the agent is genuinely stochastic (the report warns if it is not).check (
Any) – The success oracle – a registered checker name or an(episode, gold) -> boolcallable. Defaults to"output"; inject a state-based/executable oracle for a real suite, and run it isolated from the agent.metrics (
Optional[Mapping]) – Optional{name: Metric}scored on every episode againsttask.gold, with the suite’s Layer-A grammar injected – this is how the tool/argument cost weights reach atool_call/trajectorymetric. Results land inreport.detail["metrics"].price/prices – Rates for costing the episodes (see
ek.agents.cost).run (
Optional[RunProvenance]) –RunProvenancefor this run (model, simulator, suite version, scaffold). Recorded on every episode and on any baseline saved from it.seed (
Optional[int]) – Base RNG seed. Trial i of each task usesseed + i: it is recorded on the episode’s provenance and passed to the agent when the agent accepts aseedkeyword.persist/run_id/rootdir – Persist the run to the
runs/resultsstores.
- Return type:
- Returns:
A
ReliabilityReportcarryingpass@k,pass^k(with a bootstrap CI), the success rate (with a Wilson CI), the per-slice cuts, and the cost report – because reliability without cost is only half the answer.
- ek.agents.save_agent_baseline(report, name, *, rootdir=None)[source]#
Freeze a run as a named baseline (including its provenance, so the gate can guard it).
- Raises:
ValueError – if the run evaluated nothing. An empty baseline is worse than no baseline – it is a permanent free pass, because every bound in it is the maximally-uncertain default and no later run can be shown to fall below it.
- Return type:
- ek.agents.suite_grammar(tasks)[source]#
The union grammar over a whole task suite (later tools win on name collision).
- Return type:
- ek.agents.tool_grammar(*tools)[source]#
Build the Layer-A
GraphGrammarfromToolSpecs.The resulting grammar is the frozen cost SSOT every agent metric reads: node cost = the tool’s call weight, field cost = the argument’s importance.
Example
>>> from ek.base import FieldSpec >>> g = tool_grammar(ToolSpec("send", params={"to": FieldSpec("to")}, ... destructive=True)) >>> g.node_cost("send") 10.0
- Return type:
- ek.agents.trajectory_from_messages(messages)[source]#
Parse a chat transcript into a
Trajectory.Each assistant tool call becomes a
Step, and the next tool/result message becomes that step’sobservation. A tool message carrying an error marks the step as errored – error recovery across later steps is itself an evaluable signal.- Return type:
- ek.agents.wilson_interval(successes, n, *, z=1.96)[source]#
Wilson score interval for a proportion – correct in the tails, unlike Wald/CLT.
The naive normal (“Wald”) interval is badly wrong at small n and near 0/1 – exactly where agent success rates live. Wilson is the cheap, dependency-free fix.
Example
>>> lo, hi = wilson_interval(0, 10) >>> lo == 0.0, round(hi, 3) (True, 0.278)
- Return type:
Modules
Agent-evaluation data shapes: the episode (Layer B) and the task/tool grammar (Layer A). |
|
One-way adapters: turn whatever your agent already emits into an |
|
The cost model and the price SSOT: dollars per successfully completed task. |
|
The agent task-suite harness: k trials, per-slice cuts, and a gate that survives noise. |
|
LLM-as-judge: a reference-free |
|
Task-success and cost-per-successful-task: the two metrics that drive the whole thing. |
|
Tool-call correctness: BFCL-style AST matching over a multiset of calls. |
|
Trajectory (process) evaluation: a cost-weighted sequence edit distance over steps. |
|
Optional WRAP adapters: Inspect AI, uqlm, Ragas and DeepEval as ek strategies. |