Skip to content

[Feature] Add max_concurrency setting for Unsloth API inference #5481

Closed

Description

@Yur3ka

Feature request: Add configurable max_concurrency for Unsloth API inference

Summary

Please add a configurable max_concurrency setting for the Unsloth API inference server.

Currently, the API appears to process inference requests with only one active generation at a time. This is safe and stable for many local setups, but it also means users cannot explicitly increase concurrency when their hardware is capable of handling more than one request.

This feature request is not about limiting uncontrolled parallelism. Instead, it is about making the current single-concurrency behavior configurable.

Motivation

Unsloth is increasingly useful as a local API backend for external tools such as:

  • coding agents
  • IDE assistants
  • OpenAI-compatible clients
  • automation scripts
  • local multi-agent workflows
  • custom applications

Some users may run Unsloth on hardware that can handle more than one concurrent inference request, for example:

  • high-VRAM GPUs
  • multi-GPU setups
  • smaller quantized models
  • lightweight workloads
  • short completion requests
  • local services shared by multiple tools

At the moment, if the API only allows one active request at a time, every other request has to wait even when the system has enough resources to process more.

A configurable concurrency setting would let users choose the right tradeoff between stability and throughput.

Proposed solution

Add a setting such as:

UNSLOTH_API_MAX_CONCURRENCY=2

or a CLI option:

--api-max-concurrency 2

This would control how many inference requests can run at the same time.

For example:

UNSLOTH_API_MAX_CONCURRENCY=1

keeps the current safe behavior: only one inference request runs at a time.

UNSLOTH_API_MAX_CONCURRENCY=2

allows up to two inference requests to run concurrently.

UNSLOTH_API_MAX_CONCURRENCY=4

allows up to four concurrent inference requests, if the backend and hardware can support it.

Default behavior

To preserve current behavior and avoid surprising users, the default should remain:

max_concurrency = 1

This keeps Unsloth stable for most local users while allowing advanced users to opt into higher concurrency.

Suggested behavior

When the number of active inference requests is below max_concurrency, the request should start immediately.

When the number of active requests reaches max_concurrency, additional requests could either:

  1. wait in a queue until a slot becomes available, or
  2. return 429 Too Many Requests, depending on the selected policy.

A simple first implementation could use queueing by default.

Example:

max_concurrency = 2

Request behavior:

Request A -> starts immediately
Request B -> starts immediately
Request C -> waits until A or B finishes

Optional queue policy

A future option could allow users to choose how excess requests are handled:

UNSLOTH_API_QUEUE_POLICY=wait

or:

UNSLOTH_API_QUEUE_POLICY=reject

Possible modes:

wait
reject

wait

Requests above the concurrency limit wait until a slot becomes available.

reject

Requests above the concurrency limit immediately return:

429 Too Many Requests

Example response:

{
  "error": {
    "message": "API concurrency limit reached. Please retry later.",
    "type": "rate_limit_exceeded",
    "code": "max_concurrency_exceeded"
  }
}

Streaming behavior

For streaming responses, a concurrency slot should remain occupied until the stream is fully completed or cancelled.

The slot should be released when:

  • the stream finishes normally
  • the client disconnects
  • the request is cancelled
  • an exception occurs

This is important because a streaming request may keep the model busy for a long time.

Example use cases

Use case 1: Current safe behavior

A user runs Unsloth on a consumer GPU with limited VRAM.

They keep:

UNSLOTH_API_MAX_CONCURRENCY=1

Only one inference request runs at a time, matching the current behavior.

Use case 2: More capable hardware

A user runs a small quantized model on a high-VRAM GPU.

They set:

UNSLOTH_API_MAX_CONCURRENCY=2

Two short API requests can run at the same time, improving throughput.

Use case 3: External coding agents

A user connects Unsloth to an external coding agent or IDE assistant.

The tool may send multiple requests, such as:

  • chat completion
  • code edit request
  • context summarization
  • background analysis

With configurable concurrency, the user can decide whether these should run one at a time or in parallel.

Why this would be useful

This would make Unsloth API more flexible as a local inference backend.

Users with limited hardware can keep the current stable behavior.

Users with stronger hardware can increase concurrency for better throughput.

This also makes the API behavior more explicit and easier to tune for different deployment environments.

Suggested implementation idea

Internally, this could be implemented with an async semaphore or similar concurrency-control mechanism.

Example concept:

api_concurrency_semaphore = asyncio.Semaphore(settings.api_max_concurrency)

async def handle_inference_request(request):
    async with api_concurrency_semaphore:
        return await run_inference(request)

For streaming responses, the semaphore should remain acquired until the generator finishes or the client disconnects.

Backward compatibility

This should be backward compatible if the default remains:

max_concurrency = 1

That would preserve the current single-active-request behavior while allowing users to opt into higher concurrency.

Possible configuration names

Environment variable:

UNSLOTH_API_MAX_CONCURRENCY=2

CLI option:

--api-max-concurrency 2

Config file:

{
  "api": {
    "max_concurrency": 2
  }
}

Optional future improvements

This feature could later be extended with:

UNSLOTH_API_QUEUE_POLICY=wait
UNSLOTH_API_QUEUE_POLICY=reject
UNSLOTH_API_QUEUE_SIZE=5
UNSLOTH_API_REQUEST_TIMEOUT=300

Possible future additions:

  • queue size limit
  • request timeout
  • per-client concurrency limit
  • separate limits for Studio UI and external API requests
  • endpoint showing active and queued requests
  • documentation with recommended values for different hardware setups

Documentation request

Please also document:

  • the default value
  • how to configure max_concurrency
  • whether extra requests are queued or rejected
  • how streaming requests affect concurrency
  • recommended values for low-VRAM and high-VRAM setups

Expected benefit

This feature would allow Unsloth API users to explicitly control request parallelism.

It keeps the current stable single-request behavior by default, while enabling higher throughput for users who have enough hardware resources to handle concurrent inference.

Activity

  1. added
    feature request
    on May 16, 2026
  2. Imagineer99 commented on Jun 11, 2026

    @Imagineer99
    Member

    Added via --parallel / -np in #5737

  3. added a commit that references this issue on Aug 19, 2026
    b0cd167
  4. added 2 commits that reference this issue on Sep 4, 2026
    f248a76
    6f8abf5
  5. added 4 commits that reference this issue on Sep 14, 2026
    9960d2d
    327afda
    48a78cd
    00bc00d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions