BudgetStopping
BudgetStopping
Bases: Callback
Stop training, evaluation or prediction once a budget has been reached.
Running an LM program costs real money: every batch triggers inference
calls, reward computations and optimizer calls, each billed by the
provider. This callback tracks what the current program.fit(),
program.evaluate() or program.predict() run has spent so far, in
dollars and/or in tokens, and stops it as soon as one of the budgets
is reached. The check runs after every batch (training, validation,
evaluation and prediction), so a run stops mid-epoch instead of
finishing the epoch it was in when the budget ran out. Inside fit(),
validation spend counts toward the same budget and a budget hit during
validation ends both the validation pass and the training.
Spending is measured as the increase, since the run began, of the
all-time cumulated_cost and cumulated_tokens counters of every
LanguageModel and EmbeddingModel reachable from the program
(including their fallback chains). Calls made from every phase
count: inference, reward and optimizer.
The dollar cost comes from the provider's response (via LiteLLM's
response_cost). Local providers such as Ollama or vLLM don't report
a cost, so max_cost never triggers with them; use max_tokens
instead. The callback warns once when it detects that situation.
Example:
callback = synalinks.callbacks.BudgetStopping(max_cost=5.0)
# This callback will stop the training as soon as the run has spent
# more than 5 dollars across all LM and EM calls.
program = synalinks.programs.Sequential(
[synalinks.modules.Generator(data_model=Answer)])
program.compile(
synalinks.optimizers.RandomFewShot(),
reward=synalinks.rewards.ExactMatch())
history = await program.fit(
..., epochs=10, batch_size=1, callbacks=[callback], verbose=0)
# The same callback caps a standalone evaluation or prediction.
results = await program.evaluate(..., callbacks=[callback])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_cost
|
float
|
Maximum amount, in dollars, the run is
allowed to spend before stopping. |
None
|
max_tokens
|
int
|
Maximum number of tokens (prompt + completion,
summed over LM and EM calls) the run is allowed to
consume before stopping. |
None
|
verbose
|
int
|
Verbosity mode, 0 or 1. Mode 0 is silent, and mode 1
displays a message when the callback stops the run.
Defaults to |
0
|
Source code in synalinks/src/callbacks/budget_stopping.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
spent_cost
property
Dollars spent since the start of the current run.
spent_tokens
property
Tokens consumed since the start of the current run.