Add LLM failure tracing in executeLLMStream and executeLLMForToolRetry methods

- Implemented traceLLMFail method to mark LLM requests as failed in the trace context.
- Updated executeLLMStream and executeLLMForToolRetry methods to call traceLLMFail on errors, enhancing error tracking and debugging capabilities.
- Improved overall error handling in LLM execution flows, ensuring failures are properly logged in the trace.
This commit is contained in:
Max 2025-12-02 09:32:21 +08:00
parent 5ca78e0133
commit 7e8b2d8d9f
2 changed files with 18 additions and 0 deletions

View file

@ -47,6 +47,8 @@ func (ast *Assistant) executeLLMStream(
// Create LLM instance with connector and options // Create LLM instance with connector and options
llmInstance, err := llm.New(conn, completionOptions) llmInstance, err := llm.New(conn, completionOptions)
if err != nil { if err != nil {
// Mark LLM Request as failed in trace
ast.traceLLMFail(ctx, err)
return nil, err return nil, err
} }
@ -70,6 +72,8 @@ func (ast *Assistant) executeLLMStream(
log.Trace("[AGENT] LLM Stream returned: assistant=%s, err=%v", ast.ID, err) log.Trace("[AGENT] LLM Stream returned: assistant=%s, err=%v", ast.ID, err)
if err != nil { if err != nil {
log.Trace("[AGENT] Calling sendStreamEndOnError") log.Trace("[AGENT] Calling sendStreamEndOnError")
// Mark LLM Request as failed in trace
ast.traceLLMFail(ctx, err)
return nil, err return nil, err
} }
@ -108,6 +112,8 @@ func (ast *Assistant) executeLLMForToolRetry(
// Create LLM instance with connector and options // Create LLM instance with connector and options
llmInstance, err := llm.New(conn, completionOptions) llmInstance, err := llm.New(conn, completionOptions)
if err != nil { if err != nil {
// Mark LLM Retry Request as failed in trace
ast.traceLLMFail(ctx, err)
return nil, err return nil, err
} }
@ -116,6 +122,8 @@ func (ast *Assistant) executeLLMForToolRetry(
completionResponse, err := llmInstance.Stream(ctx, completionMessages, completionOptions, streamHandler) completionResponse, err := llmInstance.Stream(ctx, completionMessages, completionOptions, streamHandler)
log.Trace("[AGENT] LLM tool retry stream returned: assistant=%s, err=%v", ast.ID, err) log.Trace("[AGENT] LLM tool retry stream returned: assistant=%s, err=%v", ast.ID, err)
if err != nil { if err != nil {
// Mark LLM Retry Request as failed in trace
ast.traceLLMFail(ctx, err)
return nil, err return nil, err
} }

View file

@ -84,6 +84,16 @@ func (ast *Assistant) traceLLMComplete(ctx *context.Context, completionResponse
trace.Complete(completionResponse) trace.Complete(completionResponse)
} }
// traceLLMFail marks the LLM request as failed in the trace
func (ast *Assistant) traceLLMFail(ctx *context.Context, err error) {
trace, _ := ctx.Trace()
if trace == nil {
return
}
trace.Fail(err)
}
// traceAgentCompletion creates a completion node to report the final output // traceAgentCompletion creates a completion node to report the final output
func (ast *Assistant) traceAgentCompletion(ctx *context.Context, createResponse *context.HookCreateResponse, nextResponse *context.NextHookResponse, completionResponse *context.CompletionResponse, finalResponse interface{}) { func (ast *Assistant) traceAgentCompletion(ctx *context.Context, createResponse *context.HookCreateResponse, nextResponse *context.NextHookResponse, completionResponse *context.CompletionResponse, finalResponse interface{}) {
trace, _ := ctx.Trace() trace, _ := ctx.Trace()