src / outputBudget.ts
// Borrow only a small part of an automatic output reservation. Never reduce the
// configured context safety margin or an explicit thinking/output budget.
export function automaticOutputReduction(estimated:number,inputBudget:number,outputLimit:number,explicitMaxTokens:number,thinkingBudget:number):number {
if(explicitMaxTokens || estimated<=inputBudget)return 0;
const floor=Math.max(Math.min(outputLimit,8192),thinkingBudget?thinkingBudget+2048:0);
// Leave room for the archive protocol notice added after the first successful fit.
const reduction=Math.ceil((estimated-inputBudget+1024)/256)*256;
if(reduction>Math.min(4096,outputLimit*.25) || outputLimit-reduction<floor)return 0;
return reduction;
}