@@ -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 number maximal 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,15 +941,17 @@ 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
@@ -970,13 +974,18 @@ To override all defaults, pass all three:
970974async with Agent(
971975 ... ,
972976 middleware = [
977+ StructuredOutputRetryLimitMiddleware(0 ), # no-retries.
973978 TokenLimitMiddleware(50_000 ),
974979 StepLimitMiddleware(10 ),
975980 TimeoutLimitMiddleware(30.0 ),
976981 ],
977982) as agent: ...
978983```
979984
985+ ** Note** : When overriding limit middlewares, order matters. Place ` StructuredOutputRetryLimitMiddleware `
986+ first and ` TokenLimitMiddleware ` , ` StepLimitMiddleware ` , and ` TimeoutLimitMiddleware ` last,
987+ otherwise the limits may not behave as expected.
988+
980989There is no explicit opt-out - the intent is that agents should always have some guardrails.
981990
982991## Logger
0 commit comments