
Baeldung · 3 min readAI & machine learning
Rate Limit Information from Anthropic API Response Headers Using ChatResponseMetadata in Spring AI
Learn how to extract Anthropic API rate‑limit and token usage data in a Spring Boot application using Spring AI’s ChatResponseMetadata. The guide walks through setting up dependencies, configuring the model, and building a REST endpoint that returns both generated content and detailed rate‑limit metrics. This helps developers monitor usage and avoid quota violations.
- Add spring-ai-starter-model-anthropic to pom.xml and set api‑key, model, max‑tokens in application.properties.
- Use ChatClient.prompt().user(message).call().chatResponse() to get ChatResponse and then metadata.getRateLimit() for limits.
- Return a custom record with model, requestsLimit, requestsRemaining, tokensLimit, tokensRemaining, promptTokens, completionTokens, and content.