trace_policy
trace_policy
¶
Trace-driven router policy — learns from interaction history.
Unlike the HeuristicRouter which uses static rules, this policy
learns from accumulated traces which model/agent/tool combinations
produce the best outcomes for different query types. It maintains a
lightweight mapping of (query_class → model) that is updated
periodically from the TraceAnalyzer.
Classes¶
TraceDrivenPolicy
¶
TraceDrivenPolicy(analyzer: Optional[TraceAnalyzer] = None, *, available_models: Optional[List[str]] = None, default_model: str = '', fallback_model: str = '')
Bases: RouterPolicy
Router policy that learns from historical traces.
Maintains a mapping of query_class → best_model derived from
trace outcomes. Falls back to the provided default when no trace
data is available for a query class.
The policy is updated by calling :meth:update_from_traces, which
reads the TraceAnalyzer and recomputes the mapping.
Source code in src/openjarvis/learning/trace_policy.py
Attributes¶
policy_map
property
¶
Current learned routing decisions (read-only copy).
Functions¶
select_model
¶
select_model(context: RoutingContext) -> str
Select the best model based on learned policy or fallback.
Source code in src/openjarvis/learning/trace_policy.py
update_from_traces
¶
update_from_traces(*, since: Optional[float] = None, until: Optional[float] = None) -> Dict[str, Any]
Recompute the policy map from trace history.
Returns a summary of what changed for logging/debugging.
Source code in src/openjarvis/learning/trace_policy.py
observe
¶
Record a single observation for online (incremental) updates.
This is a lighter-weight alternative to :meth:update_from_traces
for use cases where you want to update the policy after every
interaction rather than in batch.
Source code in src/openjarvis/learning/trace_policy.py
Functions¶
classify_query
¶
Classify a query into a broad category for routing.
Source code in src/openjarvis/learning/trace_policy.py
ensure_registered
¶
Register TraceDrivenPolicy if not already present.