Skip to content

Studio: add inference only API keys - #17833

Merged
danielhanchen merged 8 commits into
mainfrom
studio-inference-only-api-keys
Nov 16, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
studio-inference-only-api-keys

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #17819

An sk-unsloth- key acts as its account on the whole Unsloth API. Someone exposing Unsloth on a LAN or behind a reverse proxy so a remote client can chat found that the same key can also load, unload and download models, flip Model auto-switch back on, and mint more keys. Turning Model auto-switch off does not stop any of that: it only decides whether a chat request naming an unloaded model loads it. There was no way to hand out a key that can only chat.

This PR lets a key be Full access (the default, exactly what every key does today) or Inference only. The create form is unchanged; the choice sits on the new token card, next to "Copy now - this won't be shown again.", and an inference only key is labelled Inference in the token list.

An inference only key:

  • reaches the same route set as keyless access in its "inference" scope (_INFERENCE_ROUTES): /v1/chat/completions, /v1/completions, /v1/embeddings, /v1/messages, /v1/responses, the token counters, GET /v1/models, GET /api/inference/loaded-models and the llama.cpp discovery probes;
  • gets 403 with a message naming the reason on everything else (/api/inference/load and its /v1/load alias, unload, downloads, settings, training, key management);
  • binds the existing LoadPolicy.NEVER, so even with Model auto-switch and auto download on, a chat request naming an unloaded model is refused instead of loading or downloading it;
  • runs with server-side tools (python, terminal, skill and memory writes) hard-disabled, as keyless access does unless the owner opts in;
  • is refused by the Unsloth Studio MCP gate, and a chat it starts mints its chat tools key as inference only too, so neither is a way around the limit.

API:

  • POST /api/auth/api-keys takes an optional access (full default, or inference); list and create responses report it.
  • PATCH /api/auth/api-keys/{id} with {"access": "full" | "inference"} changes it on an active, non-internal key of the caller's own account (the new token card uses this). It takes effect on the key's next request, and repeating it is a no-op. An inference only key cannot call it, so it cannot widen itself.

What does not change:

  • Existing keys, the internal workflow and chat tools keys, unsloth start and the desktop key are full access, as before. The new access column is NULL for every existing row and NULL reads as full.
  • The create form is byte for byte the same; every key starts as full access. Regenerate keeps the key's access.
  • The managed-account copy (account_api_keys) carries the access too, so a key restored from it after an older build's bulk delete stays inference only.
  • Any stored value other than NULL or full reads as inference only, never wider. The check runs where the key is validated, on the route the bearer was sent to; credentials built anywhere else carry no route and refuse an inference only key.

Checked on a real backend (base a21e6b7 vs this branch, same install, Model auto-switch off, offline HF cache):

Call Full key, base and head Inference only key, head
GET /v1/models 200 200
POST /v1/chat/completions (no model loaded) 400 "No model loaded" 400 "No model loaded"
POST /api/inference/load 400 (reached the loader, repo not cached offline) 403 inference only
POST /v1/load 400 (reached the loader) 403 inference only
POST /api/inference/unload 200 403 inference only
PUT /api/settings/openai-auto-switch 200 403 inference only
POST /api/auth/api-keys 200 403 inference only
chat naming an unloaded model, auto-switch and auto download ON 503 (went to look it up on Hugging Face) 404 "not downloaded yet", no lookup or download

Tests:

  • studio/backend/tests/multi_account/test_decision_api_fine_tunes.py: an inference only key keeps the configured Decision API model and gets 403 on any other.
  • studio/backend/tests/test_inference_only_api_key.py (33 cases): full keys keep every route; inference keys keep the chat routes with the never-load policy and are refused model management; legacy databases keep their keys full; unknown stored values read as inference only; create, list and PATCH round-trip the access (narrow, widen, repeat, an inference key cannot widen itself, internal and revoked keys untouched); the MCP gate refuses; the chat tools key inherits it; a managed key restored from its copy keeps it. With the auth check disabled, the route tests fail.
  • studio/frontend/tests/api-key-access.test.ts, npm run i18n:check:strict (every locale has the new strings), scripts/typecheck.mjs.

Settings > API before and after (merge base a21e6b7 vs this branch):

Create form, before and after

The create form is unchanged.

New token card, before and after

After Create token, the access choice sits next to the copy-now note; the key starts as Full access and Inference only switches it.

Token list, before and after

An inference only key is labelled Inference in the list (the API reports access: inference).

@danielhanchen danielhanchen self-assigned this Nov 15, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T08:59:12.583444Z 89e9e17 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-15T13:51:19.134859Z
We couldn’t complete this review. Comment @codex review to try again.
086e227 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 15, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 15, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 16, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 16, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 16, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3d4ffe1fe1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

def _inference_key_may_call(credentials: HTTPAuthorizationCredentials) -> bool:
from utils.keyless_api_access import KEYLESS_SCOPE_INFERENCE, scope_covers
route = getattr(credentials, "route", None)
return route is not None and scope_covers(KEYLESS_SCOPE_INFERENCE, *route)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Enforce the no-load policy on Decision API routes

When the Decision API is enabled, an inference-only key belonging to the owner can still download and switch models through POST /v1/systemone (also admitted through /mcp/decisions). Reusing the keyless allowlist admits these endpoints, but routes/systemone.py::system_one restricts checkpoint selection using request_admitted_without_credential and is_owner_context, not LoadPolicy.NEVER: this key is authenticated and retains the owner's context, so both checks permit it. _decide then reaches laya_runtime._ensure_loading, which starts a model loader without checking the load policy; its loaders can fetch uncached checkpoints. Apply the inference-only no-load restriction to this execution path, or exclude these routes until they enforce it, so a restricted key cannot bypass the model-management prohibition.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 677d983: an inference only key now gets 403 on any Decision API model other than the configured one, as a keyless caller does; test added.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 677d983f57

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +486 to +488
if record.get("key_access") == API_KEY_ACCESS_INFERENCE and not _inference_key_may_call(
credentials
):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restrict state-changing chat tools for inference-only keys

When the owner has skill-creator enabled and server-side tools are available, an inference-only key can send /v1/chat/completions with enable_tools: true and have the model call create_skill. routes/inference.py::_select_request_tools still offers that tool, and core/inference/tools.py calls skills.create_skill directly, permanently installing an enabled skill in the owner's ~/.agents/skills. Neither the route allowlist nor LoadPolicy.NEVER restricts this execution path; narrowing the key minted for Studio MCP only protects forwarded MCP calls, not built-in tools. This lets a supposedly inference-only caller change the owner's persistent agent configuration and influence subsequent chats. Apply the restricted authority to built-in tool selection/execution as well, excluding state-changing tools.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 89e9e17: an inference only key now runs with server-side tools hard-disabled (the same tools_force_disabled switch keyless access uses), so it cannot call python, terminal, create_skill or any other built-in tool; test added.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 89e9e174e4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit e80ac68 into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the studio-inference-only-api-keys branch November 16, 2026 09:26
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…(rest red on main) (#17956)

## Summary

`(Python 3.13, rest)` is red on main (seen on the staging run of
#17933):

```
studio/backend/tests/multi_account/test_alice_bob_matrix.py::test_object_route_factory_completeness
E   AssertionError: ['routes.auth:PATCH:/api-keys/{key_id}']
```

#17833 added `PATCH /api/auth/api-keys/{key_id}` (switch a key between
full and inference access). The account isolation matrix requires every
object route to have a factory, so it is exercised as owner / right user
/ wrong user / unauthenticated / deactivated, or to be listed as skipped
with a reason. The new route had neither. The route itself is scoped
correctly (`set_api_key_access` filters by the caller's key scope); it
was only missing from the matrix.

## Change

Test only: `studio/backend/tests/multi_account/factories.py` adds a
factory for the route next to the existing `DELETE /api-keys/{key_id}`
one: seed one of alice's keys, PATCH it to `{"access": "inference"}`,
and expect the response to show `inference` for alice and a 404 for
everyone else, with alice's data unchanged on every rejected call.

## Testing

- The matrix rows for both api-key routes plus the completeness /
skipped / factory checks: 15 passed. On main the completeness check
fails as above.
- Sabotage: with the key scope removed from `set_api_key_access` (any
user can change any key), the new `[owner]` and `[wrong]` rows fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Reddit issue] Remote access to the back-end API calls

1 participant