java.lang.Object
com.agentforge4j.runtime.llm.LlmCallObserver
Observes each completed LLM call: emits a
WorkflowEventType.LLM_CALL_COMPLETED event and
maintains the ReservedContextKeys.LLM_TOKENS_TOTAL running total in WorkflowState
context.
Separating this concern from AgentInvoker keeps the invoker focused on
dispatch and retry while token accounting and audit emission are independently testable.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordTyped shape of theWorkflowEventType.LLM_CALL_COMPLETEDevent payload. -
Constructor Summary
ConstructorsConstructorDescriptionLlmCallObserver(EventRecorder eventRecorder, com.fasterxml.jackson.databind.ObjectMapper objectMapper) -
Method Summary
Modifier and TypeMethodDescriptionvoidobserve(String agentId, String provider, LlmExecutionResponse response, String resolvedModel, ModelSource modelSource, ModelTier requestedModelTier, WorkflowState state, int attempt) Called once for the LLM call whose parsed output the workflow actually uses.voidrecordAttempt(String agentId, String provider, LlmExecutionResponse response, String resolvedModel, ModelSource modelSource, ModelTier requestedModelTier, WorkflowState state, int attempt) Records a discarded LLM call attempt for audit/usage purposes only.
-
Constructor Details
-
LlmCallObserver
public LlmCallObserver(EventRecorder eventRecorder, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-
-
Method Details
-
observe
public void observe(String agentId, String provider, LlmExecutionResponse response, String resolvedModel, ModelSource modelSource, ModelTier requestedModelTier, WorkflowState state, int attempt) Called once for the LLM call whose parsed output the workflow actually uses. Emits the audit event and updates theReservedContextKeys.LLM_TOKENS_TOTALrunning total.- Parameters:
agentId- the agent that triggered the callprovider- the provider name (e.g."claude")response- the full provider response including usage and model metadataresolvedModel- the model the runtime resolved and sent;nullwhen the provider default was usedmodelSource- how the model was determined (pin, tier, or provider default)requestedModelTier- the requested capability tier, ornullwhen none appliedstate- mutable run state — token total is read-add-written hereattempt- 1-based ordinal of this call within its dispatch
-
recordAttempt
public void recordAttempt(String agentId, String provider, LlmExecutionResponse response, String resolvedModel, ModelSource modelSource, ModelTier requestedModelTier, WorkflowState state, int attempt) Records a discarded LLM call attempt for audit/usage purposes only. A schema-parse-retried attempt still consumed real, metered provider tokens even though its output was rejected and superseded by a later attempt in the same dispatch. Unlikeobserve(java.lang.String, java.lang.String, com.agentforge4j.llm.api.LlmExecutionResponse, java.lang.String, com.agentforge4j.runtime.llm.ModelSource, com.agentforge4j.llm.api.ModelTier, com.agentforge4j.core.workflow.state.WorkflowState, int), this does not updateReservedContextKeys.LLM_TOKENS_TOTAL— that running total tracks only the response the workflow actually used, whichobserve(java.lang.String, java.lang.String, com.agentforge4j.llm.api.LlmExecutionResponse, java.lang.String, com.agentforge4j.runtime.llm.ModelSource, com.agentforge4j.llm.api.ModelTier, com.agentforge4j.core.workflow.state.WorkflowState, int)records exactly once per dispatch.- Parameters:
agentId- the agent that triggered the callprovider- the provider name (e.g."claude")response- the full provider response including usage and model metadataresolvedModel- the model the runtime resolved and sent;nullwhen the provider default was usedmodelSource- how the model was determined (pin, tier, or provider default)requestedModelTier- the requested capability tier, ornullwhen none appliedstate- mutable run stateattempt- 1-based ordinal of this call within its dispatch
-