Agent skill
AI observability
Use when adding Spring AI‑specific model observations, token usage, latency, externally configured cost attribution, advisor telemetry, or protected...
New to this? An agent skill is a file of instructions your AI assistant follows for one job. The text above is written for the assistant. How to use a skill
What it does
AI observability is a data skill from rrezartprebreza/
In short, it shows your assistant how to work with data step by step.
The file is 208 lines long and is set out in parts such as “Dependencies”, “Spring AI Built‑in Observability”, “Custom AI Metrics” and “Prompt/Response Logging Advisor”.
It has 6 code examples your assistant can follow.
When to use it
Reach for it when you have numbers or tables to clean, query or explain.
Everything it needs is in this one file.
It does not ask for special tools.
Safety check
Our checker read all 208 lines before we listed it.
It found no attack techniques, no hidden text and no steps that switch off your assistant's safety or send your data away.
It flagged nothing else to watch for.
The text links to 1 outside site.
It is shared under the MIT license.
Still read it before you use it, like any instructions you give your AI.
Open in the appOriginal on GitHub
What is inside
- Dependencies
- Spring AI Built‑in Observability
- Custom AI Metrics
- Prompt/Response Logging Advisor
- Cost attribution
- Structured AI Audit Log (DB)
- application.yml — Full Observability
- Gotchas
How to use it
- Claude Code: save the text as
.claude/skills/ai-observability/SKILL.mdin your project. Claude reads it when a task needs it. - Claude, ChatGPT or Gemini: tap Copy skill and paste it into the chat. It arrives with a short line asking the assistant to use it.
- Cursor and other agents: add it to the project rules or instructions.
Read the full guide to using an agent skill
Want your own? How to write an agent skill that works
The full skill
---
name: ai-observability
description: >
Use when adding Spring AI-specific model observations, token usage, latency, externally
configured cost attribution, advisor telemetry, or protected prompt and completion logging.
Use production-observability for general service metrics, health, logs, and OTLP setup.
---
# AI Observability
## Dependencies
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
```
## Spring AI Built-in Observability
Spring AI 1.0+ includes built-in Micrometer instrumentation:
```yaml
spring:
ai:
chat:
observations:
log-prompt: false # GA renamed include-prompt → log-prompt. true only in local dev (PII)
log-completion: false # GA renamed include-completion → log-completion
management:
metrics:
tags:
application: order-service
endpoints:
web:
exposure:
include: health,prometheus,metrics
```
Auto-generated metrics (OpenTelemetry GenAI semantic conventions):
- `gen_ai.client.operation` — model call latency, tagged with provider and model
- `gen_ai.client.token.usage` — token counts (input/output/total)
- `spring.ai.chat.client` — ChatClient-level operation timer/span
## Custom AI Metrics
```java
@Component
@RequiredArgsConstructor
public class AiMetrics {
private final MeterRegistry meterRegistry;
private final Timer.Builder promptTimer = Timer.builder("ai.prompt.latency")
.description("LLM prompt latency");
private final Counter.Builder tokenCounter = Counter.builder("ai.tokens.used")
.description("Total tokens consumed");
public <T> T track(String operation, String model, Supplier<T> call) {
return Timer.builder("ai.prompt.latency")
.tag("operation", operation)
.tag("model", model)
.register(meterRegistry)
.record(call); // record(Supplier) — recordCallable throws checked Exception
}
public void recordTokens(String operation, String model, int inputTokens, int outputTokens) {
Counter.builder("ai.tokens.used")
.tag("operation", operation)
.tag("model", model)
.tag("type", "input")
.register(meterRegistry)
.increment(inputTokens);
Counter.builder("ai.tokens.used")
.tag("operation", operation)
.tag("model", model)
.tag("type", "output")
.register(meterRegistry)
.increment(outputTokens);
}
}
```
## Prompt/Response Logging Advisor
GA replaced the whole advisor API: `CallAroundAdvisor` → `CallAdvisor`, `AdvisedRequest` →
`ChatClientRequest`, `AdvisedResponse` → `ChatClientResponse`, and `Usage.getGenerationTokens()` →
`getCompletionTokens()`. Agents reliably generate the old one — it does not compile on 1.0.
```java
@Component
public class AiAuditAdvisor implements CallAdvisor {
private static final Logger log = LoggerFactory.getLogger(AiAuditAdvisor.class);
@Override
public ChatClientResponse adviseCall(ChatClientRequest request, CallAdvisorChain chain) {
String requestId = UUID.randomUUID().toString();
long start = System.currentTimeMillis();
log.info("[AI-AUDIT] requestId={} promptLength={}",
requestId, request.prompt().getUserMessage().getText().length());
try {
ChatClientResponse response = chain.nextCall(request);
long latency = System.currentTimeMillis() - start;
ChatResponse chatResponse = response.chatResponse();
if (chatResponse != null && chatResponse.getMetadata() != null) {
Usage usage = chatResponse.getMetadata().getUsage();
log.info("[AI-AUDIT] requestId={} latencyMs={} inputTokens={} outputTokens={}",
requestId, latency,
usage.getPromptTokens(), usage.getCompletionTokens()); // GA: not getGenerationTokens()
}
return response;
} catch (Exception e) {
log.error("[AI-AUDIT] requestId={} FAILED after {}ms", requestId,
System.currentTimeMillis() - start, e);
throw e;
}
}
@Override
public String getName() { return "AiAuditAdvisor"; }
@Override
public int getOrder() { return Ordered.LOWEST_PRECEDENCE; }
}
```
## Cost attribution
- Keep provider prices in externally managed configuration with an effective date and currency.
- Key prices by the exact provider model identifier returned in usage metadata.
- Reject an unknown model instead of silently applying a default price.
- Preserve the raw token usage so historical costs can be recalculated after pricing changes.
- Prefer provider billing exports for invoices; application estimates are operational signals only.
## Structured AI Audit Log (DB)
The [audit advisor template](templates/AiAuditAdvisor.java) treats telemetry as best-effort.
Catch submission failures around the call to the separate async bean, as well as persistence
failures inside it: a full or stopped executor can reject a task before its method runs.
Use a bounded executor and monitor rejected audit tasks; do not use caller-runs when response
latency must remain independent of audit storage. Store sanitized error types, not raw provider
exception messages. Mandatory compliance audit records need durable delivery, not this
best-effort design.
See [Spring task execution and rejection policies](https://docs.spring.io/spring-framework/reference/integration/scheduling.html).
```java
@Entity
@Table(name = "ai_audit_log")
public class AiAuditLog {
@Id @GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private String operation;
private String model;
private int inputTokens;
private int outputTokens;
private double estimatedCostUsd;
private long latencyMs;
private boolean success;
private Instant createdAt;
}
// Async to avoid blocking main flow
@Async
public void saveAuditLog(AiAuditLog log) {
auditLogRepository.save(log);
}
```
## application.yml — Full Observability
```yaml
management:
endpoints:
web:
exposure:
include: health,prometheus,metrics,info
metrics:
distribution:
percentiles-histogram:
ai.prompt.latency: true # enables P50/P95/P99
tracing:
sampling:
probability: 1.0 # 100% trace sampling in dev, reduce in prod
logging:
level:
org.springframework.ai: DEBUG # enable in dev only
```
## Gotchas
- Agent implements `CallAroundAdvisor`/`AdvisedRequest` — removed in GA; use `CallAdvisor`/`ChatClientRequest`
- Agent calls `usage.getGenerationTokens()` — GA renamed it to `getCompletionTokens()`
- Agent logs full prompts in production — keep `log-prompt: false` for PII safety
- Agent skips async on audit saves — always `@Async` to avoid latency impact, and put the `@Async` method on a **separate bean**; calling it on `this` bypasses the proxy and runs synchronously
- Agent hardcodes token pricing — extract to config, prices change
- Agent misses failed calls in metrics — track errors separately with error tag
Copyright (c) 2026 Rrezart Prebreza.
Published under the MIT License, which you can read in full there.
Shown as published on GitHub, with credit and a link to the original.
Read it before you use it.
More skills
Agent QA browser regression
Use when verifying an existing browser journey with agent‑qa against a running Spring Boot application.
Async execution
Use when adding or debugging Spring @Async work, executor saturation, asynchronous failures or lost thread context.
Configuration properties
Use when introducing or correcting grouped Spring Boot configuration, typed property binding, validation, profiles or secret injection.
Try it in your browser
Today's AI news, one card per swipe.
Free to start.