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:
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:Expandable
Publish Raw Event
Publish an event with pre-serialized JSON payload (avoids double-serialization):Subscribe to Events
Subscribe to events on a specific topic:Expandable
Subscribe to All Events
Subscribe to all events regardless of topic:Unsubscribe
Remove a subscription:Shutdown().
Event Structure
Events have the following fields:Example: Event-Driven Cache Invalidation
Expandable
Best Practices
-
Use Appropriate Directions:
DirLocalfor metrics and debugging within a single nodeDirUpfor edge plugins sending data to controlDirDownfor control pushing updates to edges
- Handle Payload Parsing Errors: Always validate and handle JSON unmarshaling errors in event handlers.
- Avoid Blocking in Handlers: Event handlers should be fast. For heavy processing, spawn a goroutine or queue work.
-
Clean Up Subscriptions: Unsubscribe in
Shutdown()for explicit cleanup. -
Use Meaningful Topics: Use dot-separated topic names for clarity (for example,
cache.invalidate,config.updated,metrics.report). - 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 withDirLocal, 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 acreated, 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:[redacted]:
system.llm.*: the API key, and each metadata value whose name containskey,secretortoken, such as the AWS keys of a Bedrock LLMsystem.tool.*: the auth key of the tool and of its dependenciessystem.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 tokenssystem.filter.*, and the filters in tool and LLM events: the secret connection values of a guardrail, such as an API keysystem.app.*: the gateway key of the App, and the credentials of its LLMs, tools and datasourcessystem.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