@@ -613,6 +613,8 @@ triggers the retry logic described above. A custom `model_middleware` can interc
613613to observe, log, or override the retry behavior. A custom ` model_middleware ` can also raise
614614the ` StructuredOutputGenerationException ` manually to reject structured output and force a re-generation.
615615
616+ The maximal number of re-tries is limited per agent loop invocation see [ Default limit middlewares] ( #default-limit-middlewares ) .
617+
616618### Subagents with structured output/input
617619
618620In addition to output schemas, subagents can define input schemas. These schemas both constrain
@@ -926,7 +928,7 @@ async with Agent(
926928) as agent: ...
927929```
928930
929- ### Default limit middlewares
931+ ## Default limit middlewares
930932
931933Every ` Agent ` automatically applies sane default limits to prevent runaway execution
932934or excessive token usage. Default limit middlewares are appended after any user-supplied
@@ -939,22 +941,29 @@ chain - place it last if you want the same behavior.
939941| ` TokenLimitMiddleware ` | 200 000 tokens | token count of messages passed to the model |
940942| ` StepLimitMiddleware ` | 100 steps | steps taken |
941943| ` TimeoutLimitMiddleware ` | 600 seconds (10 minutes) | per ` invoke ` call |
944+ | ` StructuredOutputRetryLimitMiddleware ` | 3 retries | per ` invoke ` call |
942945
943946` TokenLimitMiddleware ` and ` StepLimitMiddleware ` check the values from the messages passed to the
944- model on each call. ` TimeoutLimitMiddleware ` resets its deadline on each ` invoke ` , so every call
945- gets a fresh time budget .
947+ model on each call. ` TimeoutLimitMiddleware ` and ` StructuredOutputRetryLimitMiddlewa ` resets its
948+ deadline/limit on each ` invoke ` , so effectively these limit only the agent loop .
946949
947950When a limit is exceeded, the agent raises the corresponding exception:
948- ` TokenLimitExceededException ` , ` StepsLimitExceededException ` , or ` TimeoutExceededException ` .
951+ ` TokenLimitExceededException ` , ` StepsLimitExceededException ` , or ` TimeoutExceededException ` ,
952+ ` StructuredOutputRetryLimitExceededException ` .
949953
950- #### Overriding defaults
954+ ### Overriding defaults
951955
952956To override a specific limit, pass your own instance of the corresponding middleware
953957class. The default for that limit is suppressed automatically - the other defaults
954958remain active:
955959
956960``` py
957- from splunklib.ai.hooks import TokenLimitMiddleware, StepLimitMiddleware, TimeoutLimitMiddleware
961+ from splunklib.ai.hooks import (
962+ TokenLimitMiddleware,
963+ StepLimitMiddleware,
964+ TimeoutLimitMiddleware,
965+ StructuredOutputRetryLimitMiddleware,
966+ )
958967
959968async with Agent(
960969 ... ,
@@ -964,19 +973,24 @@ async with Agent(
964973) as agent: ...
965974```
966975
967- To override all defaults, pass all three :
976+ To override all defaults, pass all of these to Agent's middleware list :
968977
969978``` py
970979async with Agent(
971980 ... ,
972981 middleware = [
982+ StructuredOutputRetryLimitMiddleware(0 ), # no-retries.
973983 TokenLimitMiddleware(50_000 ),
974984 StepLimitMiddleware(10 ),
975985 TimeoutLimitMiddleware(30.0 ),
976986 ],
977987) as agent: ...
978988```
979989
990+ ** Note** : When overriding limit middlewares, order matters. Place ` StructuredOutputRetryLimitMiddleware `
991+ first and ` TokenLimitMiddleware ` , ` StepLimitMiddleware ` , and ` TimeoutLimitMiddleware ` last,
992+ otherwise the limits may not behave as expected.
993+
980994There is no explicit opt-out - the intent is that agents should always have some guardrails.
981995
982996## Logger
0 commit comments