@@ -457,6 +457,140 @@ def test_gemini_count_tokens_route_reports_the_runtime_total_for_a_model_bedrock
457457 assert mantle .drain () == ()
458458
459459
460+ def test_runtime_404_on_claude_falls_back_to_mantle (counting_proxy : OwnedProxy , mantle_port : int ) -> None :
461+ gateway : Final = counting_proxy .gateway
462+ system : Final = f"{ _SYSTEM } status=404"
463+ with (
464+ wire_server (_runtime ) as runtime ,
465+ wire_server (_mantle (), port = mantle_port ) as mantle ,
466+ gateway .scenario () as scenario ,
467+ ):
468+ model : Final = _deployment (scenario , runtime .url )
469+ response : Final = _count (gateway , {** _bare (model ), "system" : system , "tools" : _TOOLS })
470+ assert _payload (response ) == {"input_tokens" : _MANTLE_COUNT }, response .text
471+ assert len (_runtime_count_targets (runtime )) == 1
472+ assert _mantle_bodies (mantle ) == (_mantle_body (system = system , tools = _TOOLS ),)
473+
474+
475+ def test_runtime_404_and_mantle_404_surface_provider_error (counting_proxy : OwnedProxy , mantle_port : int ) -> None :
476+ gateway : Final = counting_proxy .gateway
477+ system : Final = f"{ _SYSTEM } status=404"
478+ with (
479+ wire_server (_runtime ) as runtime ,
480+ wire_server (_mantle (_rejecting (404 )), port = mantle_port ) as mantle ,
481+ gateway .scenario () as scenario ,
482+ ):
483+ model : Final = _deployment (scenario , runtime .url )
484+ response : Final = _count (gateway , {** _bare (model ), "system" : system , "tools" : _TOOLS })
485+ assert response .status_code == 404 , response .text
486+ assert _REJECTION in response .text
487+ assert len (_runtime_count_targets (runtime )) == 1
488+ assert _mantle_bodies (mantle ) == (_mantle_body (system = system , tools = _TOOLS ),)
489+
490+
491+ def test_runtime_404_on_nova_does_not_call_mantle (counting_proxy : OwnedProxy , mantle_port : int ) -> None :
492+ gateway : Final = counting_proxy .gateway
493+ with (
494+ wire_server (_runtime ) as runtime ,
495+ wire_server (_mantle (), port = mantle_port ) as mantle ,
496+ gateway .scenario () as scenario ,
497+ ):
498+ model : Final = _deployment (scenario , runtime .url , _NOVA )
499+ body : Final = {** _bare (model ), "messages" : [{"role" : "user" , "content" : "status=404" }]}
500+ response : Final = _count (gateway , body )
501+ assert response .status_code == 404 , response .text
502+ assert len (_runtime_count_targets (runtime )) == 1
503+ assert mantle .drain () == ()
504+
505+
506+ def test_google_count_tokens_forwards_system_and_tools_to_mantle (
507+ counting_proxy : OwnedProxy , mantle_port : int
508+ ) -> None :
509+ gateway : Final = counting_proxy .gateway
510+ system_text : Final = "Use the provided tool"
511+ tool_schema : Final = {"type" : "object" , "properties" : {"location" : {"type" : "string" }}}
512+ body : Final [dict [str , JsonValue ]] = {
513+ "contents" : [{"role" : "user" , "parts" : [{"text" : "status=404" }]}],
514+ "systemInstruction" : {"parts" : [{"text" : system_text }]},
515+ "tools" : [
516+ {
517+ "functionDeclarations" : [
518+ {"name" : "get_weather" , "description" : "Look up weather" , "parameters" : tool_schema }
519+ ]
520+ }
521+ ],
522+ }
523+ with (
524+ wire_server (_runtime ) as runtime ,
525+ wire_server (_mantle (), port = mantle_port ) as mantle ,
526+ gateway .scenario () as scenario ,
527+ ):
528+ model : Final = _deployment (scenario , runtime .url )
529+ response : Final = gateway .request ("POST" , f"/v1beta/models/{ model } :countTokens" , body )
530+ assert _payload (response ) == {"totalTokens" : _MANTLE_COUNT , "promptTokensDetails" : []}, response .text
531+ assert len (_runtime_count_targets (runtime )) == 1
532+ assert _mantle_bodies (mantle ) == (
533+ _mantle_body (
534+ messages = [{"role" : "user" , "content" : "status=404" }],
535+ system = [{"type" : "text" , "text" : system_text }],
536+ tools = [
537+ {
538+ "name" : "get_weather" ,
539+ "description" : "Look up weather" ,
540+ "input_schema" : tool_schema ,
541+ }
542+ ],
543+ ),
544+ )
545+
546+
547+ def test_google_count_tokens_normalizes_uppercase_parameters_for_mantle (
548+ counting_proxy : OwnedProxy , mantle_port : int
549+ ) -> None :
550+ gateway : Final = counting_proxy .gateway
551+ body : Final [dict [str , JsonValue ]] = {
552+ "contents" : [{"role" : "user" , "parts" : [{"text" : "status=404" }]}],
553+ "tools" : [
554+ {
555+ "functionDeclarations" : [
556+ {
557+ "name" : "get_weather" ,
558+ "parameters" : {
559+ "type" : "OBJECT" ,
560+ "properties" : {"location" : {"type" : "STRING" }},
561+ "required" : ["location" ],
562+ },
563+ }
564+ ]
565+ }
566+ ],
567+ }
568+ with (
569+ wire_server (_runtime ) as runtime ,
570+ wire_server (_mantle (), port = mantle_port ) as mantle ,
571+ gateway .scenario () as scenario ,
572+ ):
573+ model : Final = _deployment (scenario , runtime .url )
574+ response : Final = gateway .request ("POST" , f"/v1beta/models/{ model } :countTokens" , body )
575+ assert _payload (response ) == {"totalTokens" : _MANTLE_COUNT , "promptTokensDetails" : []}, response .text
576+ assert len (_runtime_count_targets (runtime )) == 1
577+ assert _mantle_bodies (mantle ) == (
578+ _mantle_body (
579+ messages = [{"role" : "user" , "content" : "status=404" }],
580+ tools = [
581+ {
582+ "name" : "get_weather" ,
583+ "input_schema" : {
584+ "type" : "object" ,
585+ "properties" : {"location" : {"type" : "string" }},
586+ "required" : ["location" ],
587+ },
588+ }
589+ ],
590+ ),
591+ )
592+
593+
460594def test_responses_input_tokens_counts_through_mantle (counting_proxy : OwnedProxy , mantle_port : int ) -> None :
461595 gateway : Final = counting_proxy .gateway
462596 with (
0 commit comments