{"record":{"id":"e4d7598df2270ee3","repo":"JuliusBrussee/caveman","slug":"cave-nested-usage-incomplete","errorCode":"cave_nested_usage_incomplete","errorMessage":"cave_nested_usage_incomplete","messagePattern":"cave_nested_usage_incomplete","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":1583,"sourceCode":"              budgetReservation.amount,\n              \"unavailable_worst_case\",\n            );\n          }\n        }\n        if (reservation !== undefined && reservation.length > 0) {\n          markSpendIncomplete(reservation.map((item) => item.ledger));\n          spendFailure ??= failure;\n        }\n      }\n      finalMessage = message;\n    };\n    const streamFn: StreamFn = async (selected, context, streamOptions) => {\n      if (options.signal?.aborted) {\n        throw options.signal.reason ?? new Error(\"cave_run_aborted\");\n      }\n      if (spendFailure) throw spendFailure;\n      if (usageFailure) throw usageFailure;\n      if (nestedUsage.incomplete) throw new Error(\"cave_nested_usage_incomplete\");\n      if (efficiencyPlan && reasoningUsageUnavailable) {\n        throw new Error(\"cave_reasoning_usage_unavailable\");\n      }\n      if (efficiencyPlan) {\n        enforceSemanticBudgets(contextBill(lowered.ir), outputTokens, efficiencyPlan);\n        if (reasoningTokens > efficiencyPlan.budgets.reasoning) {\n          throw new Error(\"cave_reasoning_budget_exceeded\");\n        }\n      }\n      // The hard model-call ceiling is a stop condition, not a failure: ending\n      // the run through the same graceful path as every other stop keeps the\n      // partial work and the receipt intact. Checked before the\n      // increment so exactly `maxModelCalls` calls are allowed.\n      if (modelCalls >= maxModelCalls) {\n        stopReason = \"call_budget_exhausted\";\n        refusalPending = true;\n        throw new Error(\"cave_run_stopped\");\n      }","sourceCodeStart":1565,"sourceCodeEnd":1601,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L1565-L1601","documentation":"During streaming, if the accumulated usage for nested/sub-agent calls is flagged incomplete, streamFn throws this instead of producing a receipt with unaccountable spend. It guards the invariant that every token in the run is metered before budgets are enforced.","triggerScenarios":"A nested agent/sub-stream completed without reporting complete usage (nestedUsage.incomplete === true), detected at the start of the next streamFn invocation.","commonSituations":"Nested agent runs whose provider response omitted usage fields, sub-streams aborted mid-flight, or provider SDK versions that stopped returning usage on some turn types.","solutions":["Ensure nested streams complete with full usage reporting (check provider response usage fields)","Upgrade/fix the nested provider adapter so usage is always surfaced","Retry the run if the incomplete usage was caused by a transient provider issue"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// pre-flight: confirm the provider adapter surfaces usage on every turn type\nconst probe = await provider.complete({ ..., max_tokens: 1 });\nif (!probe.usage) throw new Error(\"provider does not report usage; nested runs will fail\");","typeGuard":null,"tryCatchPattern":"try { await streamAgent(opts); } catch (e) { if (e.message === \"cave_nested_usage_incomplete\") { /* retry once or drop nested sub-agent usage expectations */ } else throw e; }","preventionTips":["Keep nested provider SDKs updated so usage fields are always returned","Avoid aborting nested streams mid-flight when accounting matters","Monitor receipts for incomplete-usage flags before chaining runs"],"tags":["usage-accounting","nested-agents","billing","streaming"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}