Platform service
The platform service is a standalone REST API for SDK clients and related tools. It authenticates platform keys, records usage, returns organization-scoped platform resources, and provides a shared cache. It uses PostgreSQL for platform data and Redis for short-lived shared data. For the wider service group, see Platform services. The separate execution-support process is described in Runner service.
Service boundary
The service forms a small boundary between clients and platform data. A client sends a request with a platform key. The service validates that the key is a well-formed UUID and is active, resolves its organization, performs one platform action, and returns a result or a JSON error.
The service owns these responsibilities:
- Validate active bearer tokens before protected requests.
- Resolve the organization linked to a platform key.
- Record page and model-token usage for platform accounting.
- Return adapter, custom tool, agentic tool, and LLM profile data.
- Store, read, and delete organization-scoped values in Redis.
- Expose health and liveness checks for service management.
The service does not run workflow work. For the separate execution-support process, see Runner service.
Request flow
The request path turns a platform key into an organization-scoped action. Health requests return service status without platform-key validation. Protected requests pass through the shared authentication boundary before they access platform data. The boundary rejects malformed keys before a database query and logs only a short token prefix when it reports a key-related failure.
This diagram shows how the service routes a request to its platform or shared-data responsibility.
flowchart TD
A[SDK or tool request] --> B{Health or platform route}
B -->|health or ping| C[Return service status]
B -->|protected route| D[Read bearer token]
D --> E{Valid UUID and active platform key}
E -->|no| F[Return Unauthorized]
E -->|yes| G[Resolve organization]
G --> H{Requested capability}
H -->|usage| I[Write usage to PostgreSQL]
H -->|details or instance| J[Read platform data from PostgreSQL]
H -->|cache| K[Read or write Redis]
I --> L[Return JSON result]
J --> L
K --> L
The resolved organization scopes both durable usage records and cache keys. Database and cache resources are shared through service-level boundaries so requests can release resources after success or failure.
References: platform-service/…/platform.py• L24-145 platform-service/…/run.py• L9-19 platform-service/…/extensions.py• L34-49
Platform capabilities
The platform API groups client needs behind focused routes. Each protected capability uses the platform key to establish the caller's organization before it reads or changes shared state.
Usage records
Usage records give the platform a durable view of work performed by SDK clients. Page usage stores processed-page counts with file and run context. Token usage stores model and embedding counts with workflow, adapter, and run context. An optional subscription plugin can receive page usage after the core database write.
References: platform-service/…/platform.py• L147-214 platform-service/…/platform.py• L216-295 platform-service/…/constants.py• L8-13
Platform details
The platform-details capability lets an authenticated client confirm the organization linked to its platform key. It returns the organization identifier rather than a broader platform record.
References: platform-service/…/platform.py• L298-317
Shared cache
The cache capability gives clients a small Redis-backed key-value store. The service prefixes each key with the resolved organization identifier. This keeps one organization's cached values separate from another organization's values when clients use the same endpoint.
References: platform-service/…/platform.py• L320-377 platform-service/…/extensions.py• L12-31
Runtime instance data
Runtime instance routes return the settings needed to use platform-created resources. Adapter data comes from an organization-owned adapter instance. Custom tool, agentic tool, and LLM profile data comes from their matching platform registries. Adapter metadata is protected at rest and decoded only when the service returns the adapter details.
The adapter lookup uses both the adapter identifier and the resolved organization. A missing adapter and a deprecated adapter produce different client-facing errors. This lets clients distinguish an unavailable resource from an unsupported one.
References: platform-service/…/platform.py• L380-527 platform-service/…/adapter_instance.py• L11-52 platform-service/…/prompt_studio.py• L11-102
Application modules
The source is divided by service concern so the API boundary stays separate from data lookup and runtime setup. The main module areas have these roles:
| Module area | Service role |
|---|---|
controller | Defines health checks, platform routes, route grouping, and common JSON error behavior. |
helper | Reads the platform records needed for adapter, tool, agentic, and LLM instance data. |
config and env | Build the Flask application and load required service settings. |
extensions | Own the PostgreSQL handle, shared Redis client, and safe cursor boundary. |
constants and utils | Define shared table names, log levels, and missing-setting checks. |
| run.py | Exposes the application object and manages database connection scope around requests. |
References: platform-service/…/__init__.py• L1-64 platform-service/…/config.py• L22-55 platform-service/…/extensions.py• L1-49 platform-service/…/run.py• L1-19
Application startup
Application startup creates the Flask service, loads environment values, installs shared request behavior, configures the database, and loads optional plugins. The WSGI application is exposed as unstract.platform_service.run:app, so local and production processes use the same service object.
The startup sequence is:
- Load values from the environment and local .env file.
- Reject startup when required settings are missing.
- Set logging and common JSON error handling.
- Add request IDs and register health and platform routes.
- Configure the PostgreSQL connection.
- Load service plugins that a deployment may provide.
References: platform-service/…/config.py• L1-55 platform-service/…/env.py• L7-28 platform-service/…/utils.py• L4-40 platform-service/…/__init__.py• L11-64
Runtime configuration
The service needs access to its platform database, Redis, and the key used to protect adapter metadata. Environment values provide this configuration. The sample environment file and service README describe the operational setup, including the upload path and PostgreSQL prerequisites.
| Configuration group | Purpose |
|---|---|
| PostgreSQL settings | Locate the platform database and select its schema and application name. |
| Redis settings | Locate the shared cache service and provide optional Redis credentials. |
| Encryption setting | Decode protected adapter metadata. Keep this value stable for stored adapter data. |
| Logging setting | Select the service log level. |
| Upload and database prerequisites | Provide the local upload location and the platform databases required by the service environment. |
The service loads required values during import. A missing value therefore prevents a partial or misleading startup.
References: platform-service/…/env.py• L7-28 platform-service/…/config.py• L19-28 platform-service/README.md• L5-18
Service entrypoint
The production entrypoint starts Gunicorn on port 3001 and serves the shared Flask application. It uses gevent workers with multiple workers and threads. A development flag enables reload behavior, while the Flask task provides a simpler local debug path.
| Run mode | Entry point | Purpose |
|---|---|---|
| Production-like | platform-service/entrypoint.sh | Serve the WSGI app through Gunicorn. |
| Development server | platform-service/pyproject.toml platform-service-flask task | Run Flask locally for debugging. |
References: platform-service/entrypoint.sh• L3-41 platform-service/pyproject.toml• L48-57 platform-service/…/run.py• L1-6
Tested responsibilities
The test suite checks the service responsibilities that can fail without a clear route-level symptom. It confirms that an active UUID key passes while missing, malformed, unknown, inactive, mismatched, and database-error cases fail closed. It also confirms that authentication logs only a token prefix and that authentication decorators preserve unique route names.
Resource tests protect long-running service behavior. They show that database cursors close on query errors and not-found results. They also check that shared Redis pooling avoids a new unmanaged connection for each request and that repeated failure does not cause unbounded memory growth.
References: platform-service/…/test_auth_middleware.py• L1-111 platform-service/…/test_route_endpoints.py• L1-18 platform-service/…/test_memory_leak_simulation.py• L48-190 platform-service/…/test_memory_leak_simulation.py• L193-432 platform-service/…/README.md• L43-67
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to platform-key lookup must preserve UUID validation, fail-closed behavior, masked authentication logs, and the organization mapping used by usage records and cache key prefixes.
- Changes to protected routes must preserve the common authentication wrapper and unique Flask endpoint names.
- Changes to adapter lookup or availability rules must preserve organization scoping and the difference between missing and deprecated adapters.
- Changes to adapter encryption or
ENCRYPTION_KEYhandling must preserve access to metadata already stored with the active key. - Changes to route error handling must keep API errors in the service's JSON response contract.
- Changes to database or Redis resource ownership must preserve cleanup on success and failure paths.
- Changes to plugin loading or subscription usage must keep the core page-usage write independent from optional cloud behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing required environment setting | The application does not start with a complete service configuration. | Startup checks required settings before the service runs. |
| Missing, malformed, unknown, or inactive platform key | A protected request receives 401 Unauthorized. | Bearer-token validation checks UUID form and active database state before the route runs. |
| Database failure during key validation | Access is denied instead of being granted during an auth outage. | Token validation catches lookup errors and returns false. |
| Authentication failure logging includes a full token | A secret can leak into service logs. | Validation logs only a short token prefix and rejects malformed values before the query. |
| Platform key cannot resolve to an organization | Organization-dependent requests return an organization error instead of useful data. | Routes check the resolved organization before reading or writing data. |
| Adapter identifier is not owned by the resolved organization | The client cannot read another organization's adapter data. | Adapter lookup matches both the adapter identifier and organization UID. |
| Adapter has been deprecated | The client receives a clear error and cannot use an unsupported adapter. | Adapter lookup checks availability before returning its data. |
| Encryption key no longer matches stored adapter metadata | Adapter settings cannot be returned and adapters become inaccessible. | The service reports the key mismatch as a protected API error. |
| Database or Redis operation fails | The request returns an error rather than a successful false result. | Database transactions, safe cursors, pooled Redis access, and common error handlers contain the failure. |
| A route returns before its cursor is closed | Repeated failures can exhaust database resources. | The cursor context closes the cursor on every exit path. |
Key files
| File | Why to read it |
|---|---|
| platform-service/…/platform.py | Main client-facing platform capabilities and authentication boundary. |
| platform-service/…/health.py | Health and liveness contract used by service management. |
| platform-service/…/config.py | Application assembly, shared middleware, database setup, and plugin loading. |
| platform-service/…/run.py | WSGI application entry and request database lifecycle. |
| platform-service/…/adapter_instance.py | Organization-scoped adapter data lookup and availability checks. |
| platform-service/…/prompt_studio.py | Tool, agentic, and LLM profile data lookup. |
| platform-service/…/extensions.py | Shared PostgreSQL and Redis resource boundaries. |
| platform-service/entrypoint.sh | Production and development process launch behavior. |
| platform-service/…/test_auth_middleware.py | Authentication contract, fail-closed behavior, and safe auth logging. |
| platform-service/…/test_memory_leak_simulation.py | Resource cleanup and pooled-cache behavior under repeated failure. |