Package com.tnsai.eval.hooks
Interface EvalHook
public interface EvalHook
Hook interface for integrating evaluation metrics into agent lifecycle.
EvalHook provides callback methods that are invoked at key points during agent execution, enabling metric collection without modifying agent code.
Lifecycle Events
Agent Lifecycle:
onAgentStart()
│
▼
onBeforeChat() ─────────────────────┐
│ │
▼ │ (loop)
onBeforeToolCall() │
│ │
▼ │
onAfterToolCall() │
│ │
▼ │
onAfterChat() ◄─────────────────────┘
│
▼
onPlanGenerated()
│
▼
onPlanStepExecuted() ──┐
│ │ (loop)
▼ │
onPlanCompleted() ◄────┘
│
▼
onGoalCompleted()
│
▼
onAgentStop()
Usage Example
EvalHook metricsHook = new EvalHook() {
@Override
public void onAfterChat(EvalContext ctx, String response, long latencyMs) {
ctx.recordMetric("latency", latencyMs);
ctx.recordMetric("response_length", response.length());
}
@Override
public void onGoalCompleted(EvalContext ctx, String goalId, boolean success) {
ctx.recordMetric("goal_" + goalId, success ? 1.0 : 0.0);
}
};
agent.addEvalHook(metricsHook);
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidonAfterChat(EvalContext ctx, String response, long latencyMs) Called after processing a chat message.default voidonAfterToolCall(EvalContext ctx, String toolName, Object result, boolean success, long latencyMs) Called after executing a tool call.default voidonAgentCommunication(EvalContext ctx, String fromAgent, String toAgent, String messageType, long latencyMs) Called when inter-agent communication occurs.default voidonAgentStart(EvalContext ctx, String agentId, String sessionId) Called when agent starts a session.default voidonAgentStop(EvalContext ctx, String reason) Called when agent stops.default voidonBeforeChat(EvalContext ctx, String message) Called before processing a chat message.default voidonBeforeToolCall(EvalContext ctx, String toolName, Map<String, Object> arguments) Called before executing a tool call.default voidonError(EvalContext ctx, Throwable error, String phase) Called when an error occurs.default voidonGoalCompleted(EvalContext ctx, String goalId, boolean success, Map<String, Object> details) Called when a goal is completed (success or failure).default voidonMemoryAccess(EvalContext ctx, String operation, String key, int resultCount, long latencyMs) Called when memory is accessed.default voidonPlanCompleted(EvalContext ctx, boolean success, int totalSteps, int executedSteps, long totalLatencyMs) Called when plan execution completes (all steps finished or a step failed).default voidonPlanFailed(EvalContext ctx, String goalId, String reason) Called when plan generation fails.default voidonPlanGenerated(EvalContext ctx, String goalId, List<PlannerHandle.PlanStep> steps, long latencyMs) Called when a plan is generated for a goal.default voidonPlanStepExecuted(EvalContext ctx, String actionName, boolean success, long latencyMs) Called when a single plan step is executed.
-
Method Details
-
onAgentStart
Called when agent starts a session.- Parameters:
ctx- evaluation contextagentId- agent identifiersessionId- session identifier
-
onBeforeChat
Called before processing a chat message.- Parameters:
ctx- evaluation contextmessage- user message
-
onAfterChat
Called after processing a chat message.- Parameters:
ctx- evaluation contextresponse- agent responselatencyMs- latency in milliseconds
-
onBeforeToolCall
Called before executing a tool call.- Parameters:
ctx- evaluation contexttoolName- name of the toolarguments- tool arguments
-
onAfterToolCall
default void onAfterToolCall(EvalContext ctx, String toolName, Object result, boolean success, long latencyMs) Called after executing a tool call.- Parameters:
ctx- evaluation contexttoolName- name of the toolresult- tool resultsuccess- whether the call succeededlatencyMs- latency in milliseconds
-
onGoalCompleted
default void onGoalCompleted(EvalContext ctx, String goalId, boolean success, Map<String, Object> details) Called when a goal is completed (success or failure).- Parameters:
ctx- evaluation contextgoalId- goal identifiersuccess- whether goal was achieveddetails- additional details
-
onAgentStop
Called when agent stops.- Parameters:
ctx- evaluation contextreason- stop reason
-
onError
Called when an error occurs.- Parameters:
ctx- evaluation contexterror- the errorphase- which phase the error occurred in
-
onMemoryAccess
default void onMemoryAccess(EvalContext ctx, String operation, String key, int resultCount, long latencyMs) Called when memory is accessed.- Parameters:
ctx- evaluation contextoperation- memory operation (read/write/search)key- memory key or queryresultCount- number of resultslatencyMs- latency in milliseconds
-
onAgentCommunication
default void onAgentCommunication(EvalContext ctx, String fromAgent, String toAgent, String messageType, long latencyMs) Called when inter-agent communication occurs.- Parameters:
ctx- evaluation contextfromAgent- source agenttoAgent- target agentmessageType- type of messagelatencyMs- latency in milliseconds
-
onPlanGenerated
default void onPlanGenerated(EvalContext ctx, String goalId, List<PlannerHandle.PlanStep> steps, long latencyMs) Called when a plan is generated for a goal.- Parameters:
ctx- evaluation contextgoalId- goal identifier (empty string for all-goals planning)steps- generated plan stepslatencyMs- plan generation time in milliseconds
-
onPlanStepExecuted
default void onPlanStepExecuted(EvalContext ctx, String actionName, boolean success, long latencyMs) Called when a single plan step is executed.- Parameters:
ctx- evaluation contextactionName- the action that was executedsuccess- whether the step succeededlatencyMs- step execution time in milliseconds
-
onPlanCompleted
default void onPlanCompleted(EvalContext ctx, boolean success, int totalSteps, int executedSteps, long totalLatencyMs) Called when plan execution completes (all steps finished or a step failed).- Parameters:
ctx- evaluation contextsuccess- whether the entire plan succeededtotalSteps- total steps in the planexecutedSteps- number of steps that executed successfullytotalLatencyMs- total plan execution time in milliseconds
-
onPlanFailed
Called when plan generation fails.- Parameters:
ctx- evaluation contextgoalId- the goal planning was attempted forreason- failure reason
-