Skip to main content
Back to Community & Earn Hub
Q&A Questionsecurity +150 pt Bounty

Preventing an AI voice/chat agent from leaking internal tool stack traces when a downstream API throws 500

@liam_oconnor1 day ago 1743 views

While building our customer-facing support agent, we followed the usual advice of returning tool errors to the LLM so it can recover on its own instead of crashing the turn.

That worked great until a downstream billing microservice threw an unhandled psycopg2 exception. Our MCP wrapper caught Exception as e and returned f"Tool failed: {e}" to the model.

Instead of quietly apologizing, the model tried to be "transparent" and told the customer: "Sorry, I couldn't update your invoice because relation billing_ledger_v2 timed out on db-prod-internal."

Obviously we don't want internal table names or stack traces ever reaching the customer prompt—but if we just return "Error" to the model, it doesn't know whether to retry or fix its arguments. How are folks separating retryable LLM hints from private ops logs?

1 Answers

Community Answers

@noah_goldstein Accepted Answer1 day ago

Never pass raw str(exc) from an external-facing tool back into the LLM context. Use a two-tier error boundary inside your MCP/tool dispatcher:

  1. Log the full traceback + error_id (e.g. err_9f2a1c) to Sentry/VictoriaMetrics on the server.
  2. Map exceptions to a small allowlist of safe codes before returning to the model:
    • ValidationError -> return the safe field hint ("Invalid date format; expected YYYY-MM-DD").
    • Timeout / 503 -> return {"error_code": "UPSTREAM_UNAVAILABLE", "retryable": true, "ref": "err_9f2a1c"}.
    • Anything unexpected (KeyError, OperationalError) -> return {"error_code": "INTERNAL_ERROR", "retryable": false, "ref": "err_9f2a1c"}.

That way the model still knows whether to fix args vs retry vs apologize to the user—with zero chance of leaking internal table names or stack traces.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.