Skip to content

Feature/token limit reached exception#194

Closed
saarnilauri wants to merge 8 commits intoWordPress:trunkfrom
saarnilauri:feature/token-limit-reached-exception
Closed

Feature/token limit reached exception#194
saarnilauri wants to merge 8 commits intoWordPress:trunkfrom
saarnilauri:feature/token-limit-reached-exception

Conversation

@saarnilauri
Copy link
Contributor

This PR adds explicit token-limit handling for Anthropic so truncated outputs are surfaced as exceptions instead of normal results.

What changed

  • Added TokenLimitReachedException (src/Common/Exception/TokenLimitReachedException.php) with structured metadata:

    • maxTokens
    • providerStopReason
    • optional functionName
    • optional missingRequiredParameters
  • Updated Anthropic response parsing (src/ProviderImplementations/Anthropic/AnthropicTextGenerationModel.php) to throw TokenLimitReachedException when stop_reason is:

    • max_tokens
    • model_context_window_exceeded
  • Added tool-call diagnostics for partial argument payloads by comparing returned args with required schema fields.

  • Added Anthropic unit test scaffolding and initial coverage:

    • tests/unit/ProviderImplementations/Anthropic/MockAnthropicTextGenerationModel.php
    • tests/unit/ProviderImplementations/Anthropic/AnthropicTextGenerationModelTest.php
  • Added Anthropic integration tests for max-token scenarios (plain text + tool-calling):

    • tests/integration/Anthropic/TextGenerationIntegrationTest.php
    • tests/integration/Anthropic/FunctionCallingIntegrationTest.php

    This relates to Missing Required Parameters in Tool Calls When Token Limit Reached #193

I know that the providers will be moved to their own repositories. But as this change is still in very early stage. I decided to do this PR to the client repository, so that discussion on how this should be implemented can start.

The PR is co-authored with OpenAIs Codex 5.3 model.

@github-actions
Copy link

github-actions bot commented Feb 9, 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: 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.

@saarnilauri
Copy link
Contributor Author

@JasonTheAdams and @felixarntz

Are you still considering adding the TokenLimitReachedException?

If so, as the providers have been removed from this repository, should the TokenLimitReachedException be part of the php-ai-client or should each provider repo have it's own implementation of it?

@JasonTheAdams
Copy link
Member

Hi @saarnilauri! I just opened up #203 as a reduced version of this in light of removing the providers.

I'd appreciate it if you'd hop over there and give me some feedback. I didn't port over all the exception properties you have here because, even looking at the code, it wasn't clear to me what purpose all the properties had.

Thanks for bringing up this idea! It's a good one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants