Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): a sub-agent's credits count toward the hourly limit (CA)

purplee313fd7b75b5
diff --git a/src/api/agents/sub-agent.ts b/src/api/agents/sub-agent.ts
index 426a4d09..e06ecc1a 100644
--- a/src/api/agents/sub-agent.ts
+++ b/src/api/agents/sub-agent.ts
@@ -607,6 +607,28 @@ export async function defaultSubAgentRunner(
// not happen ("Stop means stop"). Threading the same signal the tool
// gates already use into the request makes the abort reach the wire.
const raw = await provider.chatWithTools(modelId, messages, promptTransport ? [] : llmTools, { signal: gates.abortSignal })
+ // Its credits count toward the hour like the main loop's (CA, 14.11.2026):
+ // a sub-agent on LU Cloud spent outside both limits. Past the hour limit
+ // it stops with what it found, as the main loop pauses.
+ // Accounting never breaks the delegation: a store that cannot answer
+ // (a test double, a phone without the catalogue) counts nothing.
+ let paused: string | null = null
+ try {
+ const { stepCredits, HOUR_CREDIT_LIMIT } = await import('../../lib/agent-cost-brake')
+ const { creditRatesOf, billingOf } = await import('../../lib/agent-core/credit-rates')
+ const { addHourCredits } = await import('../../lib/agent-core/credit-ledger')
+ const spent = stepCredits(creditRatesOf(laufmodell), raw.promptEvalCount, raw.evalCount, billingOf(raw))
+ if (spent > 0) {
+ const hour = addHourCredits(spent)
+ const { useSettingsStore } = await import('../../stores/settingsStore')
+ const limit = useSettingsStore.getState().settings.agentHourCreditLimit ?? HOUR_CREDIT_LIMIT
+ if (limit > 0 && hour >= limit) {
+ const material = finalContent || gesammeltesMaterial(messages)
+ paused = `Paused to protect your credits: agent runs used the hourly limit of ${limit.toLocaleString('en-US')} credits. ${material || '(no partial answer)'}`
+ }
+ }
+ } catch { /* counted nothing */ }
+ if (paused) return paused
// The calls of this turn, wherever the model put them: native, <tool_call>
// tags on the prompt transport, JSON or prose in the answer, or the
// reasoning channel. One chain for every loop (lib/agent-core/recover.ts).
diff --git a/src/lib/__tests__/sub-agent-credits-count.test.ts b/src/lib/__tests__/sub-agent-credits-count.test.ts
new file mode 100644
index 00000000..3ab1a717
--- /dev/null
+++ b/src/lib/__tests__/sub-agent-credits-count.test.ts
@@ -0,0 +1,33 @@
+/**
+ * CA, 14.11.2026: a sub-agent (delegate_task) on LU Cloud spent outside both
+ * credit limits; only the main loops fed the hour ledger. Its steps now count
+ * toward the hour, and past the hour limit it stops with what it found.
+ */
+import { describe, expect, it } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { stepCredits } from '../agent-cost-brake'
+
+describe('stepCredits', () => {
+ const rates = { inputPerMillion: 100_000, outputPerMillion: 400_000 }
+ it('prices a step from the catalogue rates', () => {
+ expect(stepCredits(rates, 10_000, 1_000)).toBe(1_000 + 400)
+ })
+ it('a Flash step and an unknown price cost nothing', () => {
+ expect(stepCredits(rates, 10_000, 1_000, 'flash')).toBe(0)
+ expect(stepCredits(null, 10_000, 1_000)).toBe(0)
+ })
+ it('a step without a count costs nothing, never a guess', () => {
+ expect(stepCredits(rates, undefined, undefined)).toBe(0)
+ })
+})
+
+describe('the sub-agent feeds the hour ledger', () => {
+ it('after every model step, and stops past the hour limit', () => {
+ const src = readFileSync('src/api/agents/sub-agent.ts', 'utf8')
+ const call = src.indexOf('const raw = await provider.chatWithTools(modelId, messages')
+ const ledger = src.indexOf('addHourCredits(spent)', call)
+ expect(ledger).toBeGreaterThan(call)
+ expect(src.slice(ledger, ledger + 600)).toMatch(/hour >= limit/)
+ expect(src).toMatch(/Paused to protect your credits: agent runs used the hourly limit/)
+ })
+})
diff --git a/src/lib/agent-cost-brake.ts b/src/lib/agent-cost-brake.ts
index 23cd57f3..2becbfdf 100644
--- a/src/lib/agent-cost-brake.ts
+++ b/src/lib/agent-cost-brake.ts
@@ -189,3 +189,20 @@ export class AgentCostBrake {
return v
}
}
+
+/**
+ * What one model step costs in credits, where the price is known and the step
+ * was billed in credits; 0 otherwise. For loops that have no brake of their
+ * own (the sub-agent), so their spending still counts toward the hour.
+ */
+export function stepCredits(
+ rates: { inputPerMillion: number; outputPerMillion: number } | null | undefined,
+ promptTokens: number | undefined,
+ outputTokens: number | undefined,
+ billing?: 'flash' | 'credits',
+): number {
+ if (!rates || billing === 'flash') return 0
+ const n = typeof promptTokens === 'number' && promptTokens > 0 ? promptTokens : 0
+ const out = typeof outputTokens === 'number' && outputTokens > 0 ? outputTokens : 0
+ return (n * rates.inputPerMillion + out * rates.outputPerMillion) / 1_000_000
+}