Like [`workflow`](Self::workflow) but with a hard token ceiling shared across every step. The cap is a best-effort *soft* cost ceiling — under a wide fan-out a few in-flight turns can race past it before the shared ledger catches up (see [`WorkflowBudget`](crate::orchestration::WorkflowBudget)).
(
&self,
limit_tokens: Option<u64>,
)
| 999 | /// wide fan-out a few in-flight turns can race past it before the shared |
| 1000 | /// ledger catches up (see [`WorkflowBudget`](crate::orchestration::WorkflowBudget)). |
| 1001 | pub fn workflow_with_token_budget( |
| 1002 | &self, |
| 1003 | limit_tokens: Option<u64>, |
| 1004 | ) -> crate::orchestration::Workflow { |
| 1005 | use crate::budget::BudgetGuard; |
| 1006 | |
| 1007 | // One shared ledger for the whole workflow, wrapping the session's own |
| 1008 | // budget guard (if any) so a host's per-tenant accounting keeps working. |
| 1009 | let mut budget = crate::orchestration::WorkflowBudget::new(limit_tokens); |
| 1010 | if let Some(inner) = self.config.budget_guard.clone() { |
| 1011 | budget = budget.with_inner(inner); |
| 1012 | } |
| 1013 | let budget = Arc::new(budget); |
| 1014 | |
| 1015 | // Install the shared ledger as the child runs' budget guard so every |
| 1016 | // step's per-turn LLM accounting feeds it. |
| 1017 | let mut parent = self.parent_run_context(); |
| 1018 | parent.budget_guard = Some(Arc::clone(&budget) as Arc<dyn BudgetGuard>); |
| 1019 | let executor: Arc<dyn crate::orchestration::AgentExecutor> = |
| 1020 | Arc::new(self.build_task_executor(parent)); |
| 1021 | |
| 1022 | let mut builder = crate::orchestration::Workflow::builder(executor) |
| 1023 | .with_root_id(format!("wf-{}", self.session_id)) |
| 1024 | .with_budget(Arc::clone(&budget)); |
| 1025 | if let Some(store) = self.session_store.clone() { |
| 1026 | builder = builder.with_store(store); |
| 1027 | } |
| 1028 | if let Some(step_events) = self.tool_context.agent_event_tx.clone() { |
| 1029 | builder = builder.with_step_events(step_events); |
| 1030 | } |
| 1031 | builder.build() |
| 1032 | } |
| 1033 | |
| 1034 | /// Build the [`ChildRunContext`](crate::child_run::ChildRunContext) that |
| 1035 | /// orchestrated / delegated child runs inherit from this session. |