0032. Gemini: the model proxy and yokla's own calls
Status: Accepted · Date: 2026-11-15
Context
The model proxy reads two families of APIs (ADR-0012): OpenAI-compatible ones, and Anthropic's Messages API. yokla's own model calls take the same two (ADR-0028).
An app on Google's Gemini models can reach them in two ways:
- Through Gemini's OpenAI-compatible endpoint. The proxy reads it already, as
openai-compatible. - Through the native API, which Google's own SDKs use:
- on AI Studio,
generativelanguage.googleapis.com, with an API key; - on Vertex AI,
aiplatform.googleapis.com, with an OAuth token.
- on AI Studio,
The native API differs from both families the proxy knows:
- the model is in the url, not the body;
- streaming is a separate method;
- messages are
contentsofparts, and tools arefunctionDeclarations; - usage is
usageMetadata, with thinking tokens apart from the output.
Through the proxy, such an app's calls are forwarded but can't be read: no model, no tokens, no prompts, no tool calls.
v0.3 plans a Gemini protocol adapter, for AI Studio and Vertex.
Decision
An upstream of kind gemini
modelProxy:
upstreams:
- id: gemini
kind: gemini
upstream: https://generativelanguage.googleapis.com- One kind for AI Studio and Vertex. Both speak the same protocol, and only their paths and credentials differ:
- AI Studio:
/v1beta/models/{model}:generateContent; - Vertex AI:
/v1/projects/{project}/locations/{location}/publishers/google/models/{model}:generateContent.
- AI Studio:
- The app's credentials pass through, as for every upstream: an
x-goog-api-keyheader, akeyquery parameter, or anAuthorizationbearer token. Records never hold one. They keep the path without its query, so not even akeyparameter. - The app points its SDK at the proxy through its base url option, at
http://127.0.0.1:<port>/<id>.
What the proxy reads
Model calls.
generateContentanswers with JSON.streamGenerateContentanswers with server-sent events (alt=sse), or else with a JSON array of the same answers. Any other call, such ascountTokensorembedContent, is forwarded and recorded with its usage unmeasured.The model.
modelVersionfrom the answer, or else the model in the url.Usage, from
usageMetadata; in a stream, from the last event that has it.yokla Gemini input promptTokenCount, plustoolUsePromptTokenCountcached input cachedContentTokenCount, part of the inputoutput candidatesTokenCount, plusthoughtsTokenCountreasoning thoughtsTokenCount, part of the outputcache write not reported: null - Input and output hold every token billed as such, as for the other kinds.
- A count the answer doesn't give is null, never 0.
Prompts (ADR-0015).
systemInstructionis the system message.- Each of
contentsis a message: roleuserstaysuser, andmodelbecomesassistant. - A message's text is that of its
textparts. Other parts, such as inline data, add none.
Tools (ADR-0025, ADR-0026).
- The tools a call offers are the names of
tools[].functionDeclarations. - The tools the answer asks for are its
functionCallparts, withargsas the arguments' JSON text. - A
functionResponsepart is a tool message that names its tool, with itsresponseas the text.modelProxy.toolResultsreads it as it reads the other kinds' tool results.
- The tools a call offers are the names of
Faults (ADR-0013)
Every fault applies to a gemini upstream, in its shapes:
- An error status has Gemini's error body:
{"error": {"code", "message", "status"}}, withRESOURCE_EXHAUSTED,INTERNALorUNAVAILABLE. - Empty content empties the
textparts of the candidates. - Invalid tool-call arguments turn a
functionCall'sargsinto text that isn't a JSON object. - Latency, a timeout, malformed JSON and a cut stream apply as they do for any upstream.
yokla's own calls
models.customer (ADR-0028) takes kind: gemini:
models:
customer:
kind: gemini
url: https://generativelanguage.googleapis.com/v1beta
model: gemini-2.5-flash
apiKeyEnv: GEMINI_API_KEY- The call.
{url}/models/{model}:generateContent, with the persona and goal assystemInstruction. - The settings.
temperature,maxOutputTokens, and the sample's seed asseed. - The key. It goes in
x-goog-api-key. Withauth: bearer, it goes as anAuthorizationbearer token instead, as Vertex AI takes it, with a url such ashttps://aiplatform.googleapis.com/v1/projects/P/locations/global/publishers/google. - Its usage is read as the proxy reads it.
The demo-shop
- The fake provider speaks Gemini too, at
/v1beta/models/{model}:generateContentand:streamGenerateContent.- It answers as it does over chat completions.
- It reports
thoughtsTokenCount, so the reasoning mapping is tested. - It records its usage in the same log.
- It plays the simulated customer there too.
- The demo-shop's model client takes
--model-protocol gemini. It then calls the native API instead of chat completions, with the same tools. - The tests.
- The proxy end-to-end test runs the acceptance with the model over HTTP in Gemini's protocol, with JSON answers and with streams. The proxy's measurement reconciles with the provider's records, call for call and token for token.
- The tool cases and one resilience matrix run in Gemini's protocol too.
- The simulated customer's test runs once with a
geminicustomer model.
Consequences
- An app on Gemini's native API is measured like one on the other two families: cost, tokens, prompts, tool calls and faults.
- One kind serves both AI Studio and Vertex. A new hosting of the same protocol needs no new kind.
- Thinking tokens count as output, as Google bills them, and show apart as reasoning.
- Embeddings on Gemini stay unmeasured until their usage is read; their calls are recorded.
- Prices are per upstream id and model, so
prices.gemini.gemini-2.5-flashas for any other kind.
Alternatives considered
- Only the OpenAI-compatible endpoint. It works today, but apps on Google's SDKs use the native API, and Vertex's native paths aren't OpenAI-compatible. Rejected.
- A kind each for AI Studio and Vertex. The protocol is the same; only the paths and credentials differ, and the proxy passes credentials through. Rejected.
- Google's SDK in yokla. yokla needs one call and a usage block, and an SDK adds supply-chain surface. Rejected.
- The Live API and other realtime sessions. They run over WebSockets, and no target needs them yet. Deferred.