Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
server: proxy cloud usage and balance APIs (#18829)
Devon Rifkinf864601538bd
diff --git a/docs/api/balance.mdx b/docs/api/balance.mdxnew file mode 100644index 000000000..2d6c40faa--- /dev/null+++ b/docs/api/balance.mdx@@ -0,0 +1,78 @@+---+title: Balance+sidebarTitle: /api/balance+openapi: /openapi.yaml GET /api/balance+---++<RequestExample>++```shell Request+curl https://ollama.com/api/balance \+ -H "Authorization: Bearer $OLLAMA_API_KEY"+```++</RequestExample>++<ResponseExample>++```json Response+{+ "included": {+ "balance_usd": 72.5,+ "allowance_usd": 100,+ "period": {+ "from": "2026-09-15T09:30:00Z",+ "until": "2026-10-15T09:30:00Z"+ }+ },+ "purchased": {+ "balance_usd": 25+ }+}+```++</ResponseExample>++This endpoint takes no query parameters. For usage history, see [Cloud usage](/api/cloud-usage).++## Balances++The included period follows your plan's monthly reset schedule, including for annual subscriptions. Period timestamps are in UTC, with `from` inclusive and `until` exclusive.++The purchased balance includes only unexpired credits.++<Accordion title="Legacy plans">++Plans with session and weekly limits return `session` and `weekly` in `included`:++```json+{+ "included": {+ "session": {+ "remaining_percent": 75,+ "resets_at": "2026-10-01T07:00:00Z"+ },+ "weekly": {+ "remaining_percent": 40,+ "resets_at": "2026-10-05T00:00:00Z"+ }+ },+ "purchased": {+ "balance_usd": 25+ }+}+```++`remaining_percent` ranges from `0` to `100`; `75` means 75% remains. `resets_at` is the next reset time in UTC.++</Accordion>++## Rate limits++This endpoint allows 10 requests per minute per user, shared across API keys and devices. We recommend polling once per minute. A `429` response includes a `Retry-After` header with the number of seconds to wait before retrying.++## Teams++All team members share the same included and purchased balances.++## Referencediff --git a/docs/api/cloud-usage.mdx b/docs/api/cloud-usage.mdxnew file mode 100644index 000000000..9fd3cf9b0--- /dev/null+++ b/docs/api/cloud-usage.mdx@@ -0,0 +1,120 @@+---+title: Cloud usage+sidebarTitle: /api/usage+openapi: /openapi.yaml GET /api/usage+---++<RequestExample>++```shell Request+curl 'https://ollama.com/api/usage?range=24h' \+ -H "Authorization: Bearer $OLLAMA_API_KEY"+```++</RequestExample>++<ResponseExample>++```json Response (22 empty buckets omitted)+{+ "range": "24h",+ "scope": "self",+ "granularity": "hour",+ "from": "2026-09-30T02:00:00Z",+ "until": "2026-10-01T02:30:00Z",+ "totals": {+ "request_count": 15,+ "usage_usd": 0.01718,+ "input_tokens": 106000,+ "cached_input_tokens": 46000,+ "output_tokens": 13600+ },+ "buckets": [+ {+ "from": "2026-09-30T12:00:00Z",+ "until": "2026-09-30T13:00:00Z",+ "request_count": 4,+ "usage_usd": 0.0052,+ "input_tokens": 24000,+ "cached_input_tokens": 0,+ "output_tokens": 3200+ },+ {+ "from": "2026-10-01T00:00:00Z",+ "until": "2026-10-01T01:00:00Z",+ "request_count": 8,+ "usage_usd": 0.0088,+ "input_tokens": 64000,+ "cached_input_tokens": 40000,+ "output_tokens": 8000+ },+ {+ "from": "2026-10-01T02:00:00Z",+ "until": "2026-10-01T02:30:00Z",+ "partial": true,+ "request_count": 3,+ "usage_usd": 0.00318,+ "input_tokens": 18000,+ "cached_input_tokens": 6000,+ "output_tokens": 2400+ }+ ]+}+```++</ResponseExample>++New usage may take time to appear.++For your remaining included and purchased credits, use [Balance](/api/balance).++Unsupported or repeated query parameters return `400`. Errors follow Ollama's [JSON error format](/api/errors#error-messages).++## Time ranges++| Range | Starts at | Bucket size | Buckets |+| ----- | ------------------------------------------------- | ----------- | ------- |+| `24h` | 24 hours before the start of the current UTC hour | Hour | 25 |+| `7d` | Seven days ago at 00:00 UTC | Day | 8 |+| `30d` | Thirty days ago at 00:00 UTC | Day | 31 |++Every range ends at the time of the request. For example, at 02:30 UTC on October 1, `24h` starts at 02:00 UTC on September 30. It returns 24 complete hourly buckets followed by the current 02:00–02:30 bucket. The `7d` and `30d` ranges return seven and thirty complete days, followed by today so far.++Buckets appear in chronological order, including zeroes for hours or days with no recorded requests. The final bucket covers the current hour or day and has `partial: true`.++All timestamps are in UTC. Each `from` is inclusive and each `until` is exclusive.++## Usage totals and buckets++`totals` summarizes usage across the entire range. `buckets` breaks that usage into hours or days.++`granularity` is set automatically: `hour` for `24h` and `day` for `7d` and `30d`. The top-level `from` and `until` describe the entire range; each bucket has its own boundaries.++`partial` indicates whether the hour or day is still in progress, rather than whether all usage has been recorded.++<Accordion title="Legacy plans">++For legacy plans, only request counts are available. If a bucket or total includes legacy requests, cost and token counts are omitted.++</Accordion>++## Rate limits++This endpoint allows 10 requests per minute per user, shared across API keys and devices. We recommend polling once per minute. A `429` response includes a `Retry-After` header with the number of seconds to wait before retrying.++## Team usage++By default, everyone sees their own usage (`scope=self`).++Team admins can use `scope=team` to see usage for the whole team:++```shell+curl 'https://ollama.com/api/usage?range=7d&scope=team' \+ -H "Authorization: Bearer $OLLAMA_API_KEY"+```++## Coming soon++We plan to add custom time ranges, usage breakdowns by model and API key, and breakdowns by team member for team admins.++## Referencediff --git a/docs/docs.json b/docs/docs.jsonindex 44a655bcd..ed96b7167 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -259,7 +259,9 @@"POST /api/pull","POST /api/push","DELETE /api/delete",- "GET /api/version"+ "GET /api/version",+ "/api/cloud-usage",+ "/api/balance"]}]diff --git a/docs/openapi.yaml b/docs/openapi.yamlindex c66ffaa40..675c8bb59 100644--- a/docs/openapi.yaml+++ b/docs/openapi.yaml@@ -1017,6 +1017,135 @@ components:type: integerminimum: 0description: Tokens generated internally for scoring, including prefix preparation and retries. May exceed the question count; not the length of the JSON response.+ UsageMetrics:+ type: object+ required: [request_count]+ properties:+ request_count:+ type: integer+ minimum: 0+ description: Number of recorded requests.+ usage_usd:+ type: number+ description: Total value of the requests in USD, including usage covered by your plan and usage paid from purchased credits.+ input_tokens:+ type: integer+ description: Recorded input tokens, including cached input tokens.+ cached_input_tokens:+ type: integer+ description: Recorded input tokens read from cache, already included in input_tokens.+ output_tokens:+ type: integer+ description: Recorded output tokens.+ UsageBucket:+ allOf:+ - $ref: "#/components/schemas/UsageMetrics"+ - type: object+ required: [from, until]+ properties:+ from:+ type: string+ format: date-time+ description: Start of the bucket in UTC, inclusive.+ until:+ type: string+ format: date-time+ description: End of the bucket in UTC, exclusive.+ partial:+ type: boolean+ description: Present and true for the current hour or day, which is still in progress.+ UsageResponse:+ type: object+ required: [range, scope, granularity, from, until, totals, buckets]+ properties:+ range:+ type: string+ enum: [24h, 7d, 30d]+ description: Selected time range.+ scope:+ type: string+ enum: [self, team]+ description: Selected usage scope.+ granularity:+ type: string+ enum: [hour, day]+ description: "Set automatically: hour for 24h, day for 7d and 30d."+ from:+ type: string+ format: date-time+ description: Start of the range in UTC, inclusive.+ until:+ type: string+ format: date-time+ description: End of the range in UTC, exclusive.+ totals:+ $ref: "#/components/schemas/UsageMetrics"+ description: Usage across the entire range.+ buckets:+ type: array+ description: Usage in chronological order, including buckets with no recorded requests.+ items:+ $ref: "#/components/schemas/UsageBucket"+ IncludedBalance:+ title: Included credits+ type: object+ required: [balance_usd, allowance_usd, period]+ properties:+ balance_usd:+ type: number+ description: Remaining included credits in USD.+ allowance_usd:+ type: number+ description: Included credits available for the full period in USD.+ period:+ type: object+ required: [from, until]+ properties:+ from:+ type: string+ format: date-time+ description: Start of the included period in UTC, inclusive.+ until:+ type: string+ format: date-time+ description: End of the included period in UTC, exclusive.+ LegacyBalanceLimit:+ type: object+ required: [remaining_percent, resets_at]+ properties:+ remaining_percent:+ type: number+ minimum: 0+ maximum: 100+ description: Percentage of the limit remaining.+ resets_at:+ type: string+ format: date-time+ description: Next reset time in UTC.+ LegacyIncludedBalance:+ title: Legacy plan limits+ type: object+ required: [session, weekly]+ properties:+ session:+ $ref: "#/components/schemas/LegacyBalanceLimit"+ weekly:+ $ref: "#/components/schemas/LegacyBalanceLimit"+ BalanceResponse:+ type: object+ required: [included, purchased]+ properties:+ included:+ oneOf:+ - $ref: "#/components/schemas/IncludedBalance"+ - $ref: "#/components/schemas/LegacyIncludedBalance"+ purchased:+ type: object+ required: [balance_usd]+ properties:+ balance_usd:+ type: number+ description: Remaining unexpired purchased credits in USD.ErrorResponse:type: objectproperties:@@ -1034,7 +1163,7 @@ paths:href: /api/systemonemetadata:title: System One- sidebarTitle: System One+ sidebarTitle: /v1/systemonecontent: |<Note>Currently available locally in Ollama.@@ -1133,6 +1262,8 @@ paths:description: Generates a response for the provided promptoperationId: generatex-mint:+ metadata:+ sidebarTitle: /api/generatehref: /api/generatex-codeSamples:- lang: bash@@ -1254,6 +1385,8 @@ paths:description: Generate the next chat message in a conversation between a user and an assistant.operationId: chatx-mint:+ metadata:+ sidebarTitle: /api/chathref: /api/chatx-codeSamples:- lang: bash@@ -1412,6 +1545,8 @@ paths:description: Creates vector embeddings representing the input textoperationId: embedx-mint:+ metadata:+ sidebarTitle: /api/embedhref: /api/embedx-codeSamples:- lang: bash@@ -1487,6 +1622,8 @@ paths:description: Fetch a list of models and their detailsoperationId: listx-mint:+ metadata:+ sidebarTitle: /api/tagshref: /api/tagsx-codeSamples:- lang: bash@@ -1520,6 +1657,8 @@ paths:description: Retrieve a list of models that are currently runningoperationId: psx-mint:+ metadata:+ sidebarTitle: /api/pshref: /api/psx-codeSamples:- lang: bash@@ -1554,6 +1693,9 @@ paths:post:summary: Show model detailsoperationId: show+ x-mint:+ metadata:+ sidebarTitle: /api/showx-codeSamples:- lang: bashlabel: Default@@ -1678,6 +1820,8 @@ paths:summary: Create a modeloperationId: createx-mint:+ metadata:+ sidebarTitle: /api/createhref: /api/createx-codeSamples:- lang: bash@@ -1790,6 +1934,8 @@ paths:summary: Copy a modeloperationId: copyx-mint:+ metadata:+ sidebarTitle: /api/copyhref: /api/copyx-codeSamples:- lang: bash@@ -1834,6 +1980,8 @@ paths:summary: Pull a modeloperationId: pullx-mint:+ metadata:+ sidebarTitle: /api/pullhref: /api/pullx-codeSamples:- lang: bash@@ -1876,6 +2024,8 @@ paths:summary: Push a modeloperationId: pushx-mint:+ metadata:+ sidebarTitle: /api/pushhref: /api/pushx-codeSamples:- lang: bash@@ -1918,6 +2068,8 @@ paths:summary: Delete a modeloperationId: deletex-mint:+ metadata:+ sidebarTitle: /api/deletehref: /api/deletex-codeSamples:- lang: bash@@ -1942,6 +2094,9 @@ paths:summary: Get versiondescription: Retrieve the version of the OllamaoperationId: version+ x-mint:+ metadata:+ sidebarTitle: /api/versionx-codeSamples:- lang: bashlabel: Default@@ -1956,3 +2111,124 @@ paths:$ref: "#/components/schemas/VersionResponse"example:version: "0.12.6"+ /api/usage:+ get:+ summary: Cloud usage+ description: Usage statistics for cloud inference, web search, and web fetch.+ operationId: usage+ servers:+ - url: https://ollama.com+ description: Ollama Cloud+ security:+ - bearerAuth: []+ parameters:+ - name: range+ in: query+ description: Completed UTC hours or days to include, plus the current partial hour or day.+ schema:+ type: string+ enum: [24h, 7d, 30d]+ default: 7d+ - name: scope+ in: query+ description: Your requests, or your team's requests. Team scope requires being a team admin.+ schema:+ type: string+ enum: [self, team]+ default: self+ responses:+ "200":+ description: Usage for the selected range and scope.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/UsageResponse"+ "400":+ description: Invalid query parameters.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "401":+ description: Invalid credentials.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "403":+ description: Account suspended or team scope requested without team admin access.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "429":+ description: Too many requests.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ headers:+ Retry-After:+ description: Seconds to wait before retrying.+ schema:+ type: integer+ minimum: 1+ "503":+ description: Usage reporting is temporarily unavailable.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ /api/balance:+ get:+ summary: Balance+ description: Check your remaining usage credits.+ operationId: balance+ servers:+ - url: https://ollama.com+ description: Ollama Cloud+ security:+ - bearerAuth: []+ responses:+ "200":+ description: Current included and purchased balances.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/BalanceResponse"+ "400":+ description: Invalid query parameters.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "401":+ description: Invalid credentials.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "403":+ description: Account suspended.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ "429":+ description: Too many requests.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"+ headers:+ Retry-After:+ description: Seconds to wait before retrying.+ schema:+ type: integer+ minimum: 1+ "503":+ description: Balance reporting is temporarily unavailable.+ content:+ application/json:+ schema:+ $ref: "#/components/schemas/ErrorResponse"diff --git a/server/cloud_proxy.go b/server/cloud_proxy.goindex 922df796c..51db28d0d 100644--- a/server/cloud_proxy.go+++ b/server/cloud_proxy.go@@ -386,9 +386,15 @@ func signCloudProxyRequest(ctx context.Context, req *http.Request) error {}func buildCloudSignatureChallenge(req *http.Request, ts string) string {- query := req.URL.Query()- query.Set("ts", ts)- req.URL.RawQuery = query.Encode()+ query, err := url.ParseQuery(req.URL.RawQuery)+ if err != nil {+ // Preserve malformed queries for the cloud to reject. Its verifier uses+ // the first timestamp, so prepend ours ahead of any client value.+ req.URL.RawQuery = "ts=" + ts + "&" + req.URL.RawQuery+ } else {+ query.Set("ts", ts)+ req.URL.RawQuery = query.Encode()+ }return fmt.Sprintf("%s,%s", req.Method, req.URL.RequestURI())}diff --git a/server/cloud_proxy_test.go b/server/cloud_proxy_test.goindex 950ec2bc2..05788bad4 100644--- a/server/cloud_proxy_test.go+++ b/server/cloud_proxy_test.go@@ -249,6 +249,30 @@ func TestBuildCloudSignatureChallengeOverwritesExistingTimestamp(t *testing.T) {}}+func TestBuildCloudSignatureChallengePreservesMalformedQuery(t *testing.T) {+ for _, rawQuery := range []string{+ "range=24h;",+ "range=24h&scope=%zz",+ "ts=999&range=24h;",+ "ts=%zz&range=24h",+ } {+ t.Run(rawQuery, func(t *testing.T) {+ req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/usage?"+rawQuery, nil)+ challenge := buildCloudSignatureChallenge(req, "123")+ wantQuery := "ts=123&" + rawQuery+ if got := req.URL.RawQuery; got != wantQuery {+ t.Errorf("signed query = %q, want %q", got, wantQuery)+ }+ if want := "GET,/api/usage?" + wantQuery; challenge != want {+ t.Errorf("challenge = %q, want %q", challenge, want)+ }+ if got := req.URL.Query().Get("ts"); got != "123" {+ t.Errorf("verifier timestamp = %q, want 123", got)+ }+ })+ }+}+func TestJSONLFramingResponseWriter_SplitsCoalescedLines(t *testing.T) {rec := &chunkRecorder{header: http.Header{}}w := &jsonlFramingResponseWriter{ResponseWriter: rec}diff --git a/server/routes.go b/server/routes.goindex 90ed6789a..addc6c189 100644--- a/server/routes.go+++ b/server/routes.go@@ -62,6 +62,8 @@ const (cloudErrRemoteModelDetailsUnavailable = "remote model details are unavailable"cloudErrWebSearchUnavailable = "web search is unavailable"cloudErrWebFetchUnavailable = "web fetch is unavailable"+ cloudErrBalanceUnavailable = "balance is unavailable"+ cloudErrUsageUnavailable = "usage is unavailable"copilotChatUserAgentPrefix = "GitHubCopilotChat/")@@ -2305,6 +2307,8 @@ func (s *Server) GenerateRoutes() (http.Handler, error) {r.DELETE("/api/delete", s.DeleteHandler)r.POST("/api/me", s.WhoamiHandler)+ r.GET("/api/balance", s.BalanceHandler)+ r.GET("/api/usage", s.UsageHandler)r.POST("/api/signout", s.SignoutHandler)// deprecated@@ -2576,6 +2580,20 @@ func (s *Server) StatusHandler(c *gin.Context) {})}+func (s *Server) BalanceHandler(c *gin.Context) {+ c.Header("Cache-Control", "private, no-store")+ // Use the account signed into this server, regardless of caller credentials.+ c.Request.Header.Del("Authorization")+ proxyCloudRequest(c, nil, cloudErrBalanceUnavailable)+}++func (s *Server) UsageHandler(c *gin.Context) {+ c.Header("Cache-Control", "private, no-store")+ // Use the account signed into this server, regardless of caller credentials.+ c.Request.Header.Del("Authorization")+ proxyCloudRequest(c, nil, cloudErrUsageUnavailable)+}+func (s *Server) WebSearchExperimentalHandler(c *gin.Context) {s.webExperimentalProxyHandler(c, "/api/web_search", cloudErrWebSearchUnavailable)}diff --git a/server/routes_reporting_test.go b/server/routes_reporting_test.gonew file mode 100644index 000000000..a420ebbb5--- /dev/null+++ b/server/routes_reporting_test.go@@ -0,0 +1,196 @@+package server++import (+ "context"+ "errors"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/gin-gonic/gin"+)++func TestReportingPassthrough(t *testing.T) {+ gin.SetMode(gin.TestMode)+ t.Setenv("OLLAMA_NO_CLOUD", "false")+ testHome := t.TempDir()+ setTestHome(t, testHome)+ writeTestOllamaPrivateKey(t, testHome)++ originalBaseURL, originalSigningHost := cloudProxyBaseURL, cloudProxySigningHost+ cloudProxySigningHost = "127.0.0.1"+ t.Cleanup(func() {+ cloudProxyBaseURL, cloudProxySigningHost = originalBaseURL, originalSigningHost+ })++ s := &Server{}+ router, err := s.GenerateRoutes()+ if err != nil {+ t.Fatal(err)+ }++ for _, tt := range []struct {+ name string+ path string+ query string+ malformed bool+ status int+ body string+ retryAfter string+ }{+ {+ name: "balance",+ path: "/api/balance",+ status: http.StatusOK,+ body: `{"purchased":{"balance_usd":25},"future_field":true}`,+ },+ {+ name: "usage",+ path: "/api/usage",+ query: "range=24h&scope=team",+ status: http.StatusOK,+ body: `{"totals":{"request_count":2},"future_field":true}`,+ },+ {+ name: "upstream error",+ path: "/api/balance",+ status: http.StatusTooManyRequests,+ body: `{"error":"rate limit exceeded"}`,+ retryAfter: "60",+ },+ {+ name: "malformed query",+ path: "/api/usage",+ query: "range=24h;",+ malformed: true,+ status: http.StatusBadRequest,+ body: `{"error":"upstream rejected malformed query"}`,+ },+ } {+ t.Run(tt.name, func(t *testing.T) {+ requests := make(chan *http.Request, 1)+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requests <- r.Clone(t.Context())+ w.Header().Set("Content-Type", "application/json")+ if tt.retryAfter != "" {+ w.Header().Set("Retry-After", tt.retryAfter)+ }+ w.WriteHeader(tt.status)+ _, _ = w.Write([]byte(tt.body))+ }))+ defer upstream.Close()+ cloudProxyBaseURL = upstream.URL++ req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, tt.path+"?ts=old&"+tt.query, nil)+ req.Header.Set("Authorization", "Bearer caller-credential")+ w := httptest.NewRecorder()+ router.ServeHTTP(w, req)++ if w.Code != tt.status || w.Body.String() != tt.body {+ t.Fatalf("response = %d %s, want %d %s", w.Code, w.Body, tt.status, tt.body)+ }+ for header, want := range map[string]string{+ "Content-Type": "application/json",+ "Cache-Control": "private, no-store",+ "Retry-After": tt.retryAfter,+ } {+ if got := w.Header().Get(header); got != want {+ t.Errorf("%s = %q, want %q", header, got, want)+ }+ }++ select {+ case forwarded := <-requests:+ if forwarded.Method != http.MethodGet || forwarded.URL.Path != tt.path {+ t.Fatalf("forwarded request = %s %s", forwarded.Method, forwarded.URL.Path)+ }+ query := forwarded.URL.Query()+ if ts := query.Get("ts"); ts == "" || ts == "old" {+ t.Errorf("expected a fresh signing timestamp, got %q", ts)+ }+ if tt.malformed {+ _, original, ok := strings.Cut(forwarded.URL.RawQuery, "&")+ if !ok || original != req.URL.RawQuery {+ t.Errorf("original query was changed: forwarded %q, original %q", forwarded.URL.RawQuery, req.URL.RawQuery)+ }+ } else {+ query.Del("ts")+ if got := query.Encode(); got != tt.query {+ t.Errorf("forwarded query = %q, want %q", got, tt.query)+ }+ }+ verifySignedOllamaRequest(t, forwarded)+ default:+ t.Fatal("reporting request was not forwarded")+ }+ })+ }+}++func TestReportingLocalErrors(t *testing.T) {+ gin.SetMode(gin.TestMode)+ setTestHome(t, t.TempDir())++ originalSignRequest := cloudProxySignRequest+ t.Cleanup(func() {+ cloudProxySignRequest = originalSignRequest+ })++ s := &Server{}+ router, err := s.GenerateRoutes()+ if err != nil {+ t.Fatal(err)+ }++ for _, endpoint := range []string{"balance", "usage"} {+ t.Run(endpoint, func(t *testing.T) {+ for _, tt := range []struct {+ name string+ method string+ noCloud string+ status int+ signCalls int+ }{+ {+ name: "cloud disabled", method: http.MethodGet, noCloud: "true",+ status: http.StatusForbidden,+ },+ {+ name: "signing failed", method: http.MethodGet, noCloud: "false",+ status: http.StatusUnauthorized, signCalls: 1,+ },+ {+ name: "method not allowed", method: http.MethodPost, noCloud: "false",+ status: http.StatusMethodNotAllowed,+ },+ } {+ t.Run(tt.name, func(t *testing.T) {+ t.Setenv("OLLAMA_NO_CLOUD", tt.noCloud)+ signCalls := 0+ cloudProxySignRequest = func(_ context.Context, req *http.Request) error {+ signCalls+++ if got := req.Header.Get("Authorization"); got != "" {+ t.Errorf("caller credentials reached signer: %q", got)+ }+ return errors.New("signing failed")+ }++ req := httptest.NewRequestWithContext(t.Context(), tt.method, "/api/"+endpoint, nil)+ req.Header.Set("Authorization", "Bearer caller-credential")+ w := httptest.NewRecorder()+ router.ServeHTTP(w, req)+ if w.Code != tt.status {+ t.Fatalf("status = %d, want %d (%s)", w.Code, tt.status, w.Body)+ }+ if tt.method == http.MethodGet && w.Header().Get("Cache-Control") != "private, no-store" {+ t.Error("reporting error response must not be cached")+ }+ if signCalls != tt.signCalls {+ t.Errorf("signer called %d times, want %d", signCalls, tt.signCalls)+ }+ })+ }+ })+ }+}