crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[FEATURE] Let LLMGuardrail use a separate LLM from the agent it judges (currently the same instance by default)

Open Alkur123 opened this issue 2 months ago • 7 comments

Feature Area

Agent capabilities

Is your feature request related to a an existing bug? Please link it here.

Related to #7150 (raised there under "Additional context"; separated out as offered).

Describe the solution you'd like

When a task or agent has a string guardrail, the LLMGuardrail that judges its output is constructed with the same BaseLLMinstance as the agent being judged. On currentmain`:

  • task.py:406, task.py:434-435, task.py:458-459: LLMGuardrail(description=..., llm=self.agent.llm)
  • agent/core.py:2109: LLMGuardrail(description=self.guardrail, llm=cast(BaseLLM, self.llm))
  • lite_agent.py:403: LLMGuardrail(description=self.guardrail, llm=self.llm)

and tasks/llm_guardrail.py:70-74 builds the judging Agent with that llm. So the judge and the judged are the same client object, not just the same model name.

What I'd like is a way to give the guardrail its own LLM, for example a guardrail_llm field on Task and Agent that is used when set and falls back to the agent's llm when not, so nothing changes for existing users. At minimum, a line in the guardrails docs saying the guardrail uses the agent's own LLM by default, and how to pass a different one.

Describe alternatives you've considered

Passing a function guardrail that wraps its own LLMGuardrail(llm=other_llm) works today, but you have to know the default shares the instance to think of doing it, and the string-guardrail shortcut is the path the docs lead people to.

Additional context

This is the "separate question" I mentioned under Additional context in #7150, split out as I offered there. It is a design question, not a vulnerability report. Nothing here lets bad output through, and I'm not claiming a bypass.

Why it matters, and I checked both points instead of assuming them:

  1. One failure domain. An outage, rate limit or bad key on that client takes out the agent and its guardrail together. That is the situation #7150 is about: during a provider incident the retries are spent while both halves are down.
  2. The judge inherits the subject's exposure. Same client, same model, same provider route and configuration. Anything that can steer the agent's model reaches the judge by the identical path.

How I checked it: I pulled LLMGuardrail._validate_output out of commit 6491f5a and ran it against a stub Agent that only records what it is given. The judging agent is built with the exact object passed in (an is check, not an equality check), and the task output is sent on that same object. A control with a different LLM does not match. Put together with the construction sites above, which pass self.agent.llm, the judge runs on the agent's own instance. The first part is executed; the construction-site part is from reading the source, and I've listed the lines so it's easy to check.

For comparison, across ten agent frameworks and guardrail toolkits I looked at, the ones that keep the check separate do it in three different ways: LlamaFirewall's scanners build their own client, Guardrails AI takes the LLM as an injected callable, and LangChain's safety middlewares don't call a model at all.

In fairness, tasks/hallucination_guardrail.py is a documented no-op in the open-source repo, so this is only about the LLMGuardrail path. If sharing the instance is intended, I'd be glad to hear the reasoning and I'll report it accurately in the write-up I'm doing.

Willingness to Contribute

I could provide more detailed specifications

Alkur123 avatar Sep 25 '26 15:09 Alkur123

Hey, this looks like a great issue. I can pick it up if it's still available.

thunderstornX avatar Sep 25 '26 17:09 thunderstornX

Hi @Alkur123 — agreed this is the useful split from #7150. Sharing the exact BaseLLM instance means one bad key/rate limit takes out both judge and judged, and a string guardrail can't easily opt out. A tiny factory gives the guardrail its own guardrail_llm and falls back to the agent's when unset:

from crewai.tools.guardrail import LLMGuardrail  # adjust import to your version

# Plug in your own judge model here (separate from the agent's LLM)
GUARDRAIL_LLM = None  # e.g. LLM(model="gpt-4o-mini", api_key=os.environ["..."])

def make_guardrail(description, agent_llm):
    """Use a dedicated judge LLM when set, else the agent's own (your #7150 ask)."""
    judge = GUARDRAIL_LLM or agent_llm
    return LLMGuardrail(description=description, llm=judge)

# guardrail = make_guardrail("output must be valid JSON", agent.llm)
if __name__ == "__main__":
    print("guardrail_llm factory ready")

chenhz01 avatar Sep 30 '26 16:09 chenhz01

Root cause in one line: all five string-guardrail construction sites (task.py ×3, agent/core.py, lite_agent.py) pass the judged agent's exact BaseLLM object into LLMGuardrail, so judge and judged share one client instance — yet a guardrail has its own cost/latency semantics and should be independently configurable.

We'd like to implement the guardrail_llm proposal from the issue: an optional field on Task and Agent that is used when set and falls back to the agent's llm when unset — fully backward compatible, with zero behavior change for existing users.

@thunderstornX — saw your offer from Sep 25 to pick this up. If you're already working on it, we'll gladly stand down and switch to reviewing your PR. Otherwise we plan to start after a 72-hour coordination window from this comment (i.e. after 2026-10-12). This is a statement of intent only — no code until the window closes.

akushonkamen avatar Oct 09 '26 00:10 akushonkamen

Thanks for coordinating. I'm starting work on the optional guardrail_llm configuration, preserving the existing agent-LLM fallback when it is unset. I'll cover the task and agent string-guardrail paths and test with separate live LLM clients before proposing a PR.

I'll link the PR here once the implementation and live evidence are ready and reviewed.

thunderstornX avatar Oct 10 '26 22:10 thunderstornX

Great to hear you're on it, @thunderstornX — we're standing down here per our coordination note.

For what it's worth, we already built an implementation with regression tests (including a dedicated test that all five string-guardrail construction sites fall back to the same agent LLM when guardrail_llm is unset, plus a serialization round-trip). If useful as a reference, the branch is up at feat/7764-guardrail-llm on our fork — feel free to reuse the backward-compat tests, entirely optional.

Looking forward to your PR, and happy to review.

akushonkamen avatar Oct 11 '26 01:10 akushonkamen

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 10 '26 12:11 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Nov 16 '26 12:11 github-actions[bot]