Skip to main content

Availability

The Event Service is available to plugins in AI Studio and on Edge Gateways. Plugins publish and subscribe to events on the event bus through ctx.Services.Events(). An event can stay on one node, or flow between the Edge Gateways and AI Studio. The SDK removes the subscriptions of a plugin when the plugin disconnects. Event subscriptions use the same broker connection as the other services, so warm up the connection first. Refer to Initialization and Connection Warmup.

Event Directions

Events have a direction that controls routing: Topics that start with system. are reserved for the object change events of AI Studio. The control plane drops system.* events that come from an edge. Gateway plugins must publish events under their own topics. From Tyk AI Studio 2.3.0, topics that start with guardrail.alert. and guardrail.outcome. are also reserved. AI Studio raises them for guardrail alerts and for guardrail outcomes: a block, a quarantine or a softening by the judge. The host refuses a plugin publish on these topics, on AI Studio and on an Edge Gateway. A Studio plugin can subscribe to them, with one topic for each subscription:
AI Studio raises these events locally (DirLocal). For Edge Gateway traffic, it raises them when the analytics pulse of the edge arrives. Gateway plugins do not receive them. For the events, refer to Guardrails.

Publish Event

Publish an event with a JSON-serializable payload:
Example:
Expandable

Publish Raw Event

Publish an event with pre-serialized JSON payload (avoids double-serialization):
Example:

Subscribe to Events

Subscribe to events on a specific topic:
Example:
Expandable

Subscribe to All Events

Subscribe to all events regardless of topic:
Example:

Unsubscribe

Remove a subscription:
Example:
Note: Subscriptions are automatically cleaned up when the plugin process terminates, but it’s good practice to explicitly unsubscribe in Shutdown().

Event Structure

Events have the following fields:

Example: Event-Driven Cache Invalidation

Expandable

Best Practices

  1. Use Appropriate Directions:
    • DirLocal for metrics and debugging within a single node
    • DirUp for edge plugins sending data to control
    • DirDown for control pushing updates to edges
  2. Handle Payload Parsing Errors: Always validate and handle JSON unmarshaling errors in event handlers.
  3. Avoid Blocking in Handlers: Event handlers should be fast. For heavy processing, spawn a goroutine or queue work.
  4. Clean Up Subscriptions: Unsubscribe in Shutdown() for explicit cleanup.
  5. Use Meaningful Topics: Use dot-separated topic names for clarity (for example, cache.invalidate, config.updated, metrics.report).
  6. Include Context in Payloads: Add timestamps, correlation IDs, or source info to payloads for debugging.

System CRUD Events

AI Studio emits a system event when an object is created, updated or deleted. AI Studio publishes these events with DirLocal, so they stay on the control plane and do not reach Edge Gateways. Any Studio plugin can subscribe to them.

Available System Events

Each object type has a created, an updated and a deleted topic, for example system.llm.created. The payloads of system.app.prompt_templates.changed, system.app.guardrail_exceptions.changed and system.secret.* carry no object, only the object ID.

Event Payload Structure

All system CRUD events use a consistent payload structure:
From Tyk AI Studio 2.3.0, literal credentials in the object show as [redacted]:
  • system.llm.*: the API key, and each metadata value whose name contains key, secret or token, such as the AWS keys of a Bedrock LLM
  • system.tool.*: the auth key of the tool and of its dependencies
  • system.datasource.*: the connection string, the connection API key and the embedder key (embed_api_key, shown as [REDACTED])
  • system.user.*: the password hash, the API key, and the session, reset and verification tokens
  • system.filter.*, and the filters in tool and LLM events: the secret connection values of a guardrail, such as an API key
  • system.app.*: the gateway key of the App, and the credentials of its LLMs, tools and datasources
  • system.group.*: the credentials of the members, and of the LLMs, datasources and tools in its catalogs
$SECRET/ and $ENV/ references show as written. Other AI Studio replicas read the same payload from the cluster event log.

Subscribing to System Events

Topics must match exactly. The event bus does not support wildcards, so Subscribe("system.app.*", ...) never receives an event. Subscribe to each topic that you need (system.app.created, system.app.updated, and so on), or use SubscribeAll and filter on ev.Topic. SubscribeAll sends every event on the bus to your handler. Check the topic first, before you decode the payload.
Expandable

Example: Audit Log Plugin

Expandable