Context that changes at runtime
Runtime context is state that is not part of the request itself, such as the time of day or the budget left, passed to evaluate as a DynamicContext so a rule can read it alongside the call's own fields.
Last updated: 14 Oct, 2026 · Agent Governance Toolkit 4.1.0
The Conditions on a tool's arguments lesson read fields that travel with the call. Some things a rule wants to read are not on the call at all: the clock, the remaining budget, the current load. evaluate takes an optional second argument, a DynamicContext, and merges its fields into the context before the rules run.
Building a DynamicContext
A DynamicContext holds up to four sub-contexts: time, cost, quota and system. Each one you fill adds its fields under a dotted prefix, so a time sub-context contributes context.time.hour, context.time.day_of_week and more. A rule reads them by that exact dotted name.
from agent_os.policies.dynamic_context import DynamicContext, TimeContext
# the evaluator never reads the clock itself; you build the hour and hand it in
runtime = DynamicContext(time=TimeContext(hour=22))A rule that reads the hour
This policy blocks an automated refund from 18:00 onward. Its condition's field is context.time.hour, the name the time sub-context contributes, compared with gte against 18.
POLICY = """
version: "1.0"
name: refund-hours
# automated refunds pause outside working hours
rules:
- name: after-hours-refund-blocked
condition: {field: context.time.hour, operator: gte, value: 18}
action: deny
priority: 10
"""The same refund at two times of day
Evaluate the identical refund twice, handing in a different hour each time. Nothing about the request changed; the runtime context did, and so did the verdict.
doc = load(POLICY)
evaluator = PolicyEvaluator(policies=[doc])
# the same refund, evaluated first at 09:00 and then at 22:00
for hour in [9, 22]:
runtime = DynamicContext(time=TimeContext(hour=hour))
d = evaluator.evaluate({"tool": "refund", "amount": 40}, dynamic_context=runtime)
print(f"{hour:02d}:00 allowed={d.allowed} rule={d.matched_rule}")09:00 allowed=True rule=None 22:00 allowed=False rule=after-hours-refund-blocked
What changed between the two calls
- At 09:00 the refund was allowed by the default action: the hour condition did not hold, no rule matched, so
matched_ruleisNone. - At 22:00 the refund was denied by
after-hours-refund-blocked, because 22 is at or above 18. - The request was byte-for-byte identical. Only the
DynamicContextdiffered, which is what makes this a runtime decision rather than a fixed one.
The sub-contexts and the fields they add
| Sub-context | Dotted fields it adds | A rule that reads it |
|---|---|---|
time | context.time.hour, .day_of_week, .timestamp | Pause an action outside working hours |
cost | context.cost.budget_remaining, .budget_used | Deny once a spend budget runs out |
quota | context.quota.api_calls_remaining | Throttle as a rate limit nears |
system | context.system.load, .error_rate | Shed load when the system is busy |
Where runtime context matters
- A time window, where an action is allowed in business hours and held otherwise.
- A spend budget, so a tool that costs money is denied once the period's budget is gone.
- A load-aware gate, where an expensive action is shed while the system is under strain.
DynamicContext and pass it on every call. A condition whose field is a plain hour instead of context.time.hour never matches, because the merged key carries the dotted prefix. TimeContext.now() fills the real clock for production, at the cost of a value that changes every run.Related
- Previous: Conditions on a tool's arguments
- Next: Risk scores
- Fill a
costsub-context and write a rule that denies the refund oncecontext.cost.budget_remainingis under 100. - Replace the fixed hour with
TimeContext.now()and watch the verdict track the real clock. - Change the condition's field to a plain
hourand confirm the rule stops matching.
You understood something today that you didn't yesterday.