From 08dacd19da1b0eefd9d274d24ed59ec1e5d5a2de Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 13 Jul 2026 23:01:10 -0400 Subject: [PATCH] refac --- backend/open_webui/main.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index d864224aa3..0136cf9a8f 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1684,6 +1684,16 @@ from open_webui.utils.anthropic import ( ) +@app.post('/api/message/count_tokens') +@app.post('/api/v1/messages/count_tokens') # Anthropic Messages token-count endpoint +async def count_message_tokens( + request: Request, + form_data: dict, + user=Depends(get_verified_user), +): + return {'input_tokens': await openai.count_anthropic_tokens(request, form_data, user)} + + @app.post('/api/message') @app.post('/api/v1/messages') # Anthropic Messages API compatible endpoint async def generate_messages( @@ -1704,9 +1714,15 @@ async def generate_messages( Authentication: Supports both standard Authorization header and Anthropic's x-api-key header (via middleware translation). """ - # Convert Anthropic payload to OpenAI format requested_model = form_data.get('model', '') + input_tokens = None + try: + input_tokens = await openai.count_anthropic_tokens(request, form_data, user) + except Exception: + # Counting must not turn a compatible generation request into an outage. + log.warning('Unable to count Anthropic input tokens for model %s', requested_model, exc_info=True) + # Convert Anthropic payload to OpenAI format openai_payload = convert_anthropic_to_openai_payload(form_data) # Route through the existing chat_completion handler