-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
This document provides a technical specification of every class, interface, method, annotation, and configuration property in AgentGuard, accompanied by concrete code examples.
- Suite Facade:
AgentGuardSuite - Suite Builder:
AgentGuardSuite.Builder - PII Masker:
PiiMasker - Reversible Context:
PiiContext - Injection Firewall:
PromptInjectionDetector - Budget Guard:
BudgetGuard - Spring Boot
@SecurePromptAnnotation - Spring Boot Configuration Properties
Package: io.github.frodygr.agentguard.core.AgentGuardSuite
The central entry point coordinating prompt sanitization, threat detection, and response restoration.
-
Signature:
public GuardedPrompt securePrompt(String rawPrompt) -
Description: Pre-processes an outgoing prompt before transmission to any LLM. Validates token budget limits, detects prompt injection threats (throwing
SecurityExceptionif detected and blocking is active), and substitutes sensitive PII with reversible surrogate tokens. -
Parameters:
-
rawPrompt: Raw user prompt or query.
-
-
Returns: An immutable
GuardedPromptrecord holdingsanitizedPrompt,piiContext, andinjectionAnalysis. -
Throws:
-
SecurityExceptionif prompt injection attack signatures are detected. -
BudgetLimitExceptionif the estimated token count exceeds the configured threshold.
-
AgentGuardSuite suite = AgentGuardSuite.ofDefaults();
AgentGuardSuite.GuardedPrompt result = suite.securePrompt("My email is alice@corp.com");
System.out.println(result.sanitizedPrompt()); // "My email is [EMAIL_1]"-
Signature:
public String restoreResponse(String llmResponse, PiiContext context) -
Description: Post-processes the text generated by the LLM, swapping surrogate tokens back into their original sensitive values using the active in-memory
PiiContext. -
Parameters:
-
llmResponse: Text received from the LLM provider. -
context:PiiContextobtained from the matchingsecurePromptcall.
-
- Returns: Natural text with real identity values restored.
String llmOutput = "Sent confirmation to [EMAIL_1].";
String finalUserText = suite.restoreResponse(llmOutput, result.piiContext());
// "Sent confirmation to alice@corp.com."-
Signature:
public void validateToolIteration(int iteration) - Description: Enforces loop boundaries on autonomous agent tool-calling loops.
-
Throws:
BudgetLimitExceptionifiterationexceedsmaxToolIterations.
int currentStep = 1;
while (agentHasMoreSteps()) {
suite.validateToolIteration(currentStep++);
agent.executeNextStep();
}Constructed via AgentGuardSuite.builder().
| Method | Parameter Type | Default | Description |
|---|---|---|---|
piiMasking(boolean) |
boolean |
true |
Enables or disables reversible PII masking. |
injectionDetection(boolean) |
boolean |
true |
Enables in-memory prompt injection heuristic analysis. |
blockOnInjection(boolean) |
boolean |
true |
When true, throws SecurityException on HIGH or CRITICAL risk. |
maxInputTokens(int) |
int |
4096 |
Max allowable input tokens per prompt. |
maxToolIterations(int) |
int |
10 |
Max autonomous agent recursion loop count. |
maxSessionTokens(int) |
int |
50000 |
Max accumulated tokens before tripping budget circuit breaker. |
AgentGuardSuite customSuite = AgentGuardSuite.builder()
.piiMasking(true)
.injectionDetection(true)
.blockOnInjection(true)
.maxInputTokens(2048)
.maxToolIterations(5)
.build();Package: io.github.frodygr.agentguard.core.pii.PiiMasker
Stand-alone anonymization engine supporting seven sensitive categories:
| Category | Identifier Pattern | Validation Method |
|---|---|---|
| RFC 5322 regex | Standard format check | |
| Credit Card | 13-19 digit card formats | Luhn Checksum Algorithm (ISO/IEC 7812) |
| IBAN | European Bank Account Numbers | ISO 13616 pattern |
| Tax ID / DNI / SSN | Spanish DNI/NIE and US SSN | Check digit & structure |
| Phone Number | International (+34, +1, etc.) | 9+ digit length validation |
| Secrets / JWTs |
eyJ... Bearer tokens |
JWT structure validation |
| IPv4 Address | Dotted quad addresses | Validates octets 0-255 (excludes localhost) |
PiiMasker masker = new PiiMasker();
PiiMasker.MaskResult maskResult = masker.mask("Transfer funds to ES9121000418450200051332");Package: io.github.frodygr.agentguard.core.pii.PiiContext
Thread-safe bidirectional store keeping token substitutions in memory:
-
getOrCreateToken(String original, PiiType type): Allocates or returns surrogate token ([TYPE_N]). -
resolveOriginal(String token): Looks up original sensitive string. -
hasMaskedEntities():booleanindicating if any items were anonymized. -
totalMaskedCount(): Number of unique sensitive values captured. -
getMappings(): Returns unmodifiable map oftoken -> original.
Package: io.github.frodygr.agentguard.core.injection.PromptInjectionDetector
PromptInjectionDetector detector = new PromptInjectionDetector();
InjectionResult result = detector.analyze(userPrompt);
if (!result.safe()) {
System.err.println("Threat Level: " + result.risk());
System.err.println("Patterns: " + result.detectedPatterns());
}Package: io.github.frodygr.agentguard.core.budget.BudgetGuard
Enforces financial boundaries and prevents denial-of-wallet:
BudgetGuard guard = new BudgetGuard(4096, 8, 25000);
guard.validateInputPrompt(longPrompt); // Throws BudgetLimitException if oversizedPackage: io.github.frodygr.agentguard.spring.annotation.SecurePrompt
Place on Spring @Service or @Component methods:
@Service
public class ChatService {
@SecurePrompt(maskPii = true, detectInjection = true, blockOnThreat = true)
public String askAi(String prompt) {
// 'prompt' is sanitized before method begins
return llmClient.chat(prompt);
}
}Configure via application.yml:
agentguard:
enabled: true
pii-masking: true
injection-detection: true
block-on-injection: true
max-input-tokens: 4096
max-tool-iterations: 10
max-session-tokens: 50000AgentGuard • Enterprise AI Security, PII Masking & Prompt Firewall • GitHub