|
| 1 | +# Observability Guide |
| 2 | + |
| 3 | +KODE exposes observability as SDK capabilities first, not as an application server. |
| 4 | + |
| 5 | +That means the SDK gives you structured metrics, observations, persistence hooks, and OTEL bridging. Your application decides whether to expose them through HTTP, dashboards, alerting, or internal admin tools. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## What KODE Includes |
| 10 | + |
| 11 | +- Runtime metrics via `agent.getMetricsSnapshot()` |
| 12 | +- Runtime observation reads via `agent.getObservationReader()` |
| 13 | +- Runtime observation streaming via `agent.subscribeObservations()` |
| 14 | +- Optional persisted observation queries via `observability.persistence` |
| 15 | +- Optional OTEL export via `observability.otel` |
| 16 | + |
| 17 | +## What KODE Deliberately Does Not Include |
| 18 | + |
| 19 | +- Built-in HTTP server lifecycle |
| 20 | +- Built-in auth, tenant isolation, or rate limiting |
| 21 | +- Built-in observability dashboard UI |
| 22 | +- Opinionated public API contracts for app delivery |
| 23 | + |
| 24 | +Those concerns belong in your application layer. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Runtime Metrics and Observations |
| 29 | + |
| 30 | +Use runtime readers when you want to inspect the current agent process without waiting for external exports. |
| 31 | + |
| 32 | +```typescript |
| 33 | +const metrics = agent.getMetricsSnapshot(); |
| 34 | +const reader = agent.getObservationReader(); |
| 35 | + |
| 36 | +const latest = reader.listObservations({ |
| 37 | + kinds: ['generation', 'tool'], |
| 38 | + limit: 20, |
| 39 | +}); |
| 40 | + |
| 41 | +for await (const envelope of agent.subscribeObservations({ runId: metrics.currentRunId })) { |
| 42 | + console.log(envelope.observation.kind, envelope.observation.name); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Typical runtime uses: |
| 47 | + |
| 48 | +- show "live now" generation/tool activity in an admin panel |
| 49 | +- inspect approval waits, tool errors, and compression events |
| 50 | +- derive counters without polling raw event buses |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Persisted Observations |
| 55 | + |
| 56 | +Use persisted readers when you need history, audit views, or process-restart durability. |
| 57 | + |
| 58 | +```typescript |
| 59 | +import { |
| 60 | + Agent, |
| 61 | + JSONStoreObservationBackend, |
| 62 | + createStoreBackedObservationReader, |
| 63 | +} from '@shareai-lab/kode-sdk'; |
| 64 | + |
| 65 | +const observationBackend = new JSONStoreObservationBackend('./.kode-observability'); |
| 66 | + |
| 67 | +const agent = await Agent.create({ |
| 68 | + templateId: 'assistant', |
| 69 | + observability: { |
| 70 | + persistence: { |
| 71 | + backend: observationBackend, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}, deps); |
| 75 | + |
| 76 | +const persistedReader = createStoreBackedObservationReader(observationBackend); |
| 77 | +const history = await persistedReader.listObservations({ |
| 78 | + agentIds: [agent.agentId], |
| 79 | + kinds: ['agent_run', 'generation', 'tool'], |
| 80 | + limit: 50, |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +Use persisted storage for: |
| 85 | + |
| 86 | +- audit timelines |
| 87 | +- run replay pages |
| 88 | +- offline analytics jobs |
| 89 | +- debugging after process restart |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## OTEL Bridge |
| 94 | + |
| 95 | +If your platform already standardizes on OpenTelemetry, enable the bridge and ship translated spans to your collector. |
| 96 | + |
| 97 | +```typescript |
| 98 | +const agent = await Agent.create({ |
| 99 | + templateId: 'assistant', |
| 100 | + observability: { |
| 101 | + otel: { |
| 102 | + enabled: true, |
| 103 | + serviceName: 'kode-agent', |
| 104 | + exporter: { |
| 105 | + protocol: 'http/json', |
| 106 | + endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT!, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | +}, deps); |
| 111 | +``` |
| 112 | + |
| 113 | +Keep KODE's native observation model as your source of truth. OTEL is best treated as an interoperability/export path. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Data Safety and Capture Boundaries |
| 118 | + |
| 119 | +KODE supports configurable capture levels through `observability.capture`: |
| 120 | + |
| 121 | +- `off` |
| 122 | +- `summary` |
| 123 | +- `full` |
| 124 | +- `redacted` |
| 125 | + |
| 126 | +Prefer `summary` or `redacted` for production unless you have a clear compliance reason to store more detail. |
| 127 | + |
| 128 | +Also note: |
| 129 | + |
| 130 | +- provider-specific raw payloads are not part of the public observation schema |
| 131 | +- debug-only extensions may appear under `metadata.__debug` |
| 132 | +- `metadata.__debug` should be treated as internal/private and filtered before external exposure |
| 133 | + |
| 134 | +This keeps the public observation model safer and more stable. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Exposing Observability over HTTP |
| 139 | + |
| 140 | +If you need HTTP endpoints, build them in your app on top of the SDK readers/backends. |
| 141 | + |
| 142 | +Reference example: |
| 143 | + |
| 144 | +- `examples/08-observability-http.ts` |
| 145 | +- run with `npm run example:observability-http` |
| 146 | + |
| 147 | +That example demonstrates: |
| 148 | + |
| 149 | +- a normal app-owned HTTP server |
| 150 | +- `POST /agents/demo/send` to drive an agent run |
| 151 | +- `GET /api/observability/.../metrics` for runtime metrics |
| 152 | +- `GET /api/observability/.../observations/runtime` for live observation reads |
| 153 | +- `GET /api/observability/.../observations/persisted` for persisted history |
| 154 | + |
| 155 | +This boundary is intentional: the SDK provides observability primitives, while the app owns transport, auth, and presentation. |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## Recommended Rollout |
| 160 | + |
| 161 | +1. Start with runtime metrics and runtime observation readers. |
| 162 | +2. Add persisted observation storage for auditability. |
| 163 | +3. Add OTEL export only if your platform needs centralized telemetry. |
| 164 | +4. Add app-layer HTTP or UI only after the data model and filtering policy are clear. |
| 165 | + |
| 166 | +This order keeps the SDK integration stable and avoids prematurely coupling KODE to one delivery surface. |
0 commit comments