diff --git a/docs/docs/Configuration/environment-variables.md b/docs/docs/Configuration/environment-variables.md
index a2fb4c544..76e2d1a91 100644
--- a/docs/docs/Configuration/environment-variables.md
+++ b/docs/docs/Configuration/environment-variables.md
@@ -191,6 +191,7 @@ The following table lists the environment variables supported by Langflow.
| LANGFLOW_DB_MAX_OVERFLOW | Integer | `20` | **DEPRECATED:** Use LANGFLOW_DB_CONNECTION_SETTINGS instead. The number of connections to allow that can be opened beyond the pool size. |
| LANGFLOW_DB_CONNECT_TIMEOUT | Integer | `20` | The number of seconds to wait before giving up on a lock to be released or establishing a connection to the database. |
| LANGFLOW_DB_CONNECTION_SETTINGS | JSON | Not set | A JSON dictionary to centralize database connection parameters. Example: `{"pool_size": 10, "max_overflow": 20}` |
+| LANGFLOW_DISABLE_TRACK_APIKEY_USAGE | Boolean | `false` | If set to `true`, disables tracking of API key usage (`total_uses` and `last_used_at`) to avoid database contention under high concurrency. |
| LANGFLOW_ENABLE_LOG_RETRIEVAL | Boolean | `false` | Enable log retrieval functionality. |
| LANGFLOW_FALLBACK_TO_ENV_VAR | Boolean | `true` | If enabled, [global variables](../Configuration/configuration-global-variables.md) set in the Langflow UI fall back to an environment variable with the same name when Langflow fails to retrieve the variable value. |
| LANGFLOW_FRONTEND_PATH | String | `./frontend` | Path to the frontend directory containing build files. This is for development purposes only. See [`--frontend-path` option](./configuration-cli.md#run-frontend-path). |
diff --git a/src/backend/base/langflow/services/database/models/api_key/crud.py b/src/backend/base/langflow/services/database/models/api_key/crud.py
index 22a52a561..31dcae568 100644
--- a/src/backend/base/langflow/services/database/models/api_key/crud.py
+++ b/src/backend/base/langflow/services/database/models/api_key/crud.py
@@ -1,4 +1,3 @@
-import asyncio
import datetime
import secrets
from typing import TYPE_CHECKING
@@ -10,7 +9,7 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from langflow.services.database.models import User
from langflow.services.database.models.api_key import ApiKey, ApiKeyCreate, ApiKeyRead, UnmaskedApiKeyRead
-from langflow.services.deps import session_scope
+from langflow.services.deps import get_settings_service, session_scope
if TYPE_CHECKING:
from sqlmodel.sql.expression import SelectOfScalar
@@ -50,17 +49,14 @@ async def delete_api_key(session: AsyncSession, api_key_id: UUID) -> None:
await session.commit()
-update_total_uses_tasks: set[asyncio.Task] = set()
-
-
async def check_key(session: AsyncSession, api_key: str) -> User | None:
"""Check if the API key is valid."""
query: SelectOfScalar = select(ApiKey).options(selectinload(ApiKey.user)).where(ApiKey.api_key == api_key)
api_key_object: ApiKey | None = (await session.exec(query)).first()
if api_key_object is not None:
- task = asyncio.create_task(update_total_uses(api_key_object.id))
- task.add_done_callback(update_total_uses_tasks.discard)
- update_total_uses_tasks.add(task)
+ settings_service = get_settings_service()
+ if settings_service.settings.disable_track_apikey_usage is not True:
+ await update_total_uses(api_key_object.id)
return api_key_object.user
return None
diff --git a/src/backend/base/langflow/services/settings/base.py b/src/backend/base/langflow/services/settings/base.py
index 918e3197a..3be436802 100644
--- a/src/backend/base/langflow/services/settings/base.py
+++ b/src/backend/base/langflow/services/settings/base.py
@@ -134,6 +134,7 @@ class Settings(BaseSettings):
prometheus_port: int = 9090
"""The port on which Langflow will expose Prometheus metrics. 9090 is the default port."""
+ disable_track_apikey_usage: bool = False
remove_api_keys: bool = False
components_path: list[str] = []
langchain_cache: str = "InMemoryCache"