Skip to content

trading_agent.gateway.routes.system_routes

Routes for the gateway itself: model usage and the configuration.

GET /api/usage today's tokens per desk and backend, and the latest rate-limit reports GET /api/configuration trading_agent.toml as the gateway read it, with every known secret masked

Typical usage example:

application.include_router(SystemRoutes(guard, configuration, usage, redactor, desk_names).router())

SystemRoutes

SystemRoutes(guard, configuration, usage, redactor, desk_names)

Bases: RouteGroup

The usage and configuration routes.

Creates the routes.

Parameters:

Name Type Description Default
guard AccessGuard

Checks the access token.

required
configuration AgentConfiguration

The configuration the gateway runs with.

required
usage UsageTracker

Today's model usage.

required
redactor SecretRedactor

Masks secrets in the configuration.

required
desk_names Sequence[str]

The running desks.

required
Source code in src/trading_agent/gateway/routes/system_routes.py
def __init__(
    self,
    guard: access_guard.AccessGuard,
    configuration: agent_configuration.AgentConfiguration,
    usage: usage_tracker.UsageTracker,
    redactor: secret_redactor.SecretRedactor,
    desk_names: Sequence[str],
):
    """Creates the routes.

    Args:
        guard (access_guard.AccessGuard): Checks the access token.
        configuration (agent_configuration.AgentConfiguration): The configuration the gateway runs with.
        usage (usage_tracker.UsageTracker): Today's model usage.
        redactor (secret_redactor.SecretRedactor): Masks secrets in the configuration.
        desk_names (Sequence[str]): The running desks.
    """
    super().__init__(guard)
    self._configuration = configuration
    self._usage = usage
    self._redactor = redactor
    self._desk_names = list(desk_names)

usage

usage()

Today's model usage.

Returns:

Type Description
dict[str, Any]

For each desk, the calls, tokens and cost per backend, and each backend's latest rate-limit report.

Source code in src/trading_agent/gateway/routes/system_routes.py
def usage(self) -> dict[str, Any]:
    """Today's model usage.

    Returns:
        dict[str, Any]: For each desk, the calls, tokens and cost per backend, and each backend's latest rate-limit report.
    """
    desks = {}
    for desk_name in self._desk_names:
        backends = {}
        for backend_name, counts in self._usage.today(desk_name).items():
            backends[backend_name] = dataclasses.asdict(counts)
        desks[desk_name] = backends
    return {
        'desks': desks,
        'rate_limits': self._usage.rate_limits(),
    }

configuration

configuration()

The configuration, with secrets masked.

Returns:

Type Description
dict[str, Any]

Every setting of trading_agent.toml, including defaults, as JSON values.

Source code in src/trading_agent/gateway/routes/system_routes.py
def configuration(self) -> dict[str, Any]:
    """The configuration, with secrets masked.

    Returns:
        dict[str, Any]: Every setting of trading_agent.toml, including defaults, as JSON values.
    """
    settings = self._configuration.model_dump(mode='json')
    return self._redactor.redact(settings)