Monitoring
When trypatch recovers a call, the caller sees a success, so the failure behind it never reaches your usual error handling. Callbacks and logging let you report it yourself: to an error tracker (Sentry, Datadog, Bugsnag, ...), a metric, or your own logs.
onInvestigationStart
onInvestigationStart runs when the method fails, before the investigation. It gets the InvestigationContext, with the original error as ctx.error.
export class PricingService {
@trypatch({
onInvestigationStart: ctx => {
Sentry.captureException(ctx.error, { tags: { 'trypatch.method': ctx.methodName } })
},
aiInvestigation: {
resultSchema: QuoteSchema,
investigationProvider: { provider: 'openai', apiKey: process.env.OPENAI_API_KEY! },
},
})
async getQuote (productId: string): Promise<Quote> {
return await pricingClient.fetchQuote(productId)
}
}onAiInvestigationEnd
onAiInvestigationEnd runs once the AI's outcome has been applied, before the method returns or rejects. It gets the same context and a ResolvedOutcome: the value the method returns, or the error the outcome ended with (a custom or fallback error, or whatever failed while applying it, such as a result tool throwing), each with the AI's explanation.
aiInvestigation: {
resultSchema: QuoteSchema,
investigationProvider: { provider: 'openai', apiKey: process.env.OPENAI_API_KEY! },
onAiInvestigationEnd: (ctx, outcome) => {
if (outcome.type === 'result') {
metrics.increment('pricing.quote_recovered')
return
}
Sentry.captureException(ctx.error, {
tags: { 'trypatch.method': ctx.methodName },
extra: { investigationError: outcome.error.message, explanation: outcome.explanation },
})
},
},Callback behavior
- Awaited. Callbacks can be async, and the method's result waits for them.
onAiInvestigationEndcounts towardstimeoutMs. - A throw breaks the flow. It's handled like a failed investigation: logged, and the method rethrows its original error. For
onInvestigationStartthat skips the investigation; foronAiInvestigationEndit discards a recovered value. Catch errors inside the callback if reporting must never affect the call. - Not redacted. See Redaction.
Logging
trypatch logs through logging.logger and logs nothing without one.
- Investigations that end without a value are logged at
errorlevel, including those that fail before the AI picks an outcome (provider error, timeout, invalid response), whenonAiInvestigationEndisn't called. Forwarderrorto your monitoring to catch them. - Unavailable MCP servers and failed Cursor cancellations are logged at
warnlevel. - Each outcome and its explanation are logged at
infolevel. verbosity: 'low'logs each message only, without error objects or the explanation.