Skip to content

Adds TokenLimitReachedException for providers#203

Merged
JasonTheAdams merged 2 commits intotrunkfrom
add/token-limit-exception
Feb 16, 2026
Merged

Adds TokenLimitReachedException for providers#203
JasonTheAdams merged 2 commits intotrunkfrom
add/token-limit-exception

Conversation

@JasonTheAdams
Copy link
Copy Markdown
Member

Summary

  • Adds TokenLimitReachedException for providers to throw when a prompt exceeds the model's context window
    or a configured token maximum
    • Includes an optional $maxTokens parameter so callers catching the exception can programmatically
      inspect the limit that was hit (e.g., to retry with a shorter prompt or surface a meaningful error to end
      users)

This is a smaller follow up to #194, as the providers are no longer bundled. I didn't bring over all the parameters for the exception in that PR because I wasn't sure what they were all for. Happy to add them back if there's strong reasoning!

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 13, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: JasonTheAdams <jason_the_adams@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Copy Markdown
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one thing to fix pre merge.

* exceeds the allowed limit, whether that is the model's context window
* or a configured maximum.
*
* @since 0.4.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use n.e.x.t throughout the PR

@felixarntz felixarntz added the [Type] Enhancement A suggestion for improvement. label Feb 14, 2026
@felixarntz felixarntz added this to the 0.5.0 milestone Feb 14, 2026
@saarnilauri
Copy link
Copy Markdown
Contributor

saarnilauri commented Feb 16, 2026

@JasonTheAdams here is explanation for the parameters that I added in #194

I added $providerStopReason as reaching max tokens is not the only case when AI cuts of output. There is also the case for the when the Anthropic API return model_context_window_exceeded as a stop reason. I thought this param can help the developers to identify different cases.

The idea for $functionName was to highlight if the max tokens was the stop reason when the AI was doing a tool call and the output was cut out. I thought that this could help developers help to identify a tool call that makes AI output tokens over the limit.

The idea for $missingRequiredParameters is more detailed addition to $functionName. It would list all the required parameters for the tool call that was interupted due reaching the token limit. When Anthropic API is forming the parameters for the tool to be used and the token limit runs full the AI return the tool call but with only the parameters that is was able to fully generate. So, $missingRequiredParameters could help the developer to understand what part of the tool call is consuming the tokens and are missing.

@JasonTheAdams
Copy link
Copy Markdown
Member Author

Thanks for the context, @saarnilauri! Those do make sense, but feel a bit nice-to-have at the moment. Let's start simple and gather more feedback over time.

@JasonTheAdams JasonTheAdams merged commit 61f46ac into trunk Feb 16, 2026
6 checks passed
@JasonTheAdams JasonTheAdams deleted the add/token-limit-exception branch February 16, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants