Frameworks
NestJS injectable service
Construct the Klaro instance once as a private field on the service -- every method on the class shares the same retry/redaction/budget config.
← All recipes
import { Injectable } from "@nestjs/common";
import { Controller, Post, Body } from "@nestjs/common";
import { Klaro, retries, secrets, budget } from "@klaroshield/sdk";
import OpenAI from "openai";
@Injectable()
export class ChatService {
private openai = new OpenAI();
private klaro = new Klaro()
.use(retries({ max: 3 }))
.use(budget({ maxMonthlyUsd: 50 }))
.use(secrets({ mode: "mask" }));
private chat = this.klaro.wrap(
this.openai.chat.completions.create.bind(this.openai.chat.completions)
);
async ask(messages: { role: string; content: string }[]) {
return this.chat({ model: "gpt-4o-mini", messages });
}
}
@Controller("chat")
export class ChatController {
constructor(private readonly chatService: ChatService) {}
@Post()
async chat(@Body() body: { messages: { role: string; content: string }[] }) {
return this.chatService.ask(body.messages);
}
}Checked against the shipped @klaroshield/sdk API. Full docs at klaro.services/klaroshield/docs.