Skip to main content

Rerank and filter an agent's search results

What you'll do: give the Trip planner agent two extra search stages. A reranker puts the closest passages in order of relevance. A decision model then drops the passages that do not help answer the question, so the agent never reads them.

A knowledge base search returns the passages whose embeddings are closest to the query. Close is not the same as useful. A search for a winter trip with snow also returns the Lisbon and Kyoto trips, because the base has only three trips and the search fills every slot. The model then has to work out for itself that two of the three passages are about something else.

This guide builds on the agent from Run an agent with a knowledge base and a tool. Any agent with a documents knowledge base works the same way.

Before you start​

You need two provider keys:

  • A Jina API key for the reranker. Jina bills the rerank calls to that key.
  • An OpenRouter API key for the decision model. The decision model calls OpenRouter's Decisions API, so the connection must be an OpenAI-compatible one whose base URL ends in /v1, such as https://openrouter.ai/api/v1.

Add each key under Gateway → Credentials: choose the provider Jina for the first, and OpenAI-compatible with the OpenRouter base URL for the second. Route calls through the gateway shows the credential form.

1. Register a rerank model​

Open Gateway → Models and click New model:

  • Public name: jina-reranker. The agent's Reranker list shows this name.
  • Credential: your Jina credential.
  • Upstream model: jina-reranker-v3. Jina has no model list, so type the id and choose Use jina-reranker-v3 as typed.
  • Kind: Rerank. A Jina credential offers only this kind.
  • Input $/1M: your price per million tokens on your Jina plan. Usage and traces show each call's cost from it, and a blank price counts as $0.

The New model dialog with the public name jina-reranker, the Jina credential, the upstream model jina-reranker-v3 and the kind Rerank

Click Register model.

2. Register a decision model​

Click New model again:

  • Public name: jev-decider.
  • Credential: your OpenRouter credential.
  • Upstream model: typesafe/jev-1.14.
  • Kind: Decision.
  • Input $/1M and Output $/1M: leave both blank to use the cost OpenRouter reports for each call, or enter your own prices.

The New model dialog with the public name jev-decider, the OpenRouter credential, the upstream model typesafe/jev-1.14 and the kind Decision

Click Register model.

Do this in code

Both models are registered with POST /api/v1/gateway/models and a kind of rerank or decision. The gateway models reference has the request and response. The SDKs list models but do not register them.

3. Set the search settings on the agent​

Open Agents → Trip planner. Under Knowledge, the Trip itineraries card holds the settings this agent uses when it searches that base. Another agent that searches the same base keeps its own settings.

  • Results to return: keep 3.
  • Reranker: jina-reranker. The search fetches a wider set of candidates, and the reranker keeps the best 3 in order of relevance.
  • Decision model: jev-decider. A Threshold field appears, set to 0.5. The decision model rates each passage from 0 to 1 for how likely it is to help answer the question. A passage rated below the threshold is dropped.

The Trip itineraries card with Results to return 3, the reranker jina-reranker, the decision model jev-decider and a threshold of 0.5

4. Test the settings before you save​

The box at the bottom of the card runs a search with the settings on screen, saved or not. Type a winter trip with snow and click Test.

Each passage shows three numbers: Vector is the similarity score from the search, Rerank is the reranker's score, and Relevant is the decision model's rating. The Lapland trip is kept with a rating of 97%. The Lisbon and Kyoto trips are listed under Dropped by the decision model with ratings near 10%, so the agent does not see them.

The test result: lapland-northern-lights.md kept with Vector 0.449, Rerank 0.182 and Relevant 97%, and lisbon-coast.md under Dropped by the decision model at 10%

If a passage you need is dropped, lower the threshold and test again. Then click Save. When you save a new reranker or decision model, AcruxCore sends it one test call first and refuses the save with the provider's message if the call fails.

Do this in code

search_detailed (Python) and searchDetailed (Node) run the same test. They return the kept passages, the dropped ones, and whether each stage ran. agents.update saves the settings; its knowledge_bases list replaces the agent's whole list.

model_ids = {m.public_name: m.id for m in await hub.models.list()}
kb = next(k for k in await hub.knowledge_bases.list() if k.name == "Trip itineraries")

result = await hub.knowledge_bases.search_detailed(
kb.id,
"a winter trip with snow",
top_k=3,
rerank_model_id=model_ids["jina-reranker"],
decider_model_id=model_ids["jev-decider"],
)
for hit in result.hits:
print(f"kept {hit.filename} rerank {hit.rerank_score:.3f} relevant {hit.decision:.0%}")
for hit in result.dropped:
print(f"dropped {hit.filename} rerank {hit.rerank_score:.3f} relevant {hit.decision:.0%}")

agent = next(a for a in await hub.agents.list() if a.name == "Trip planner")
await hub.agents.update(
agent.id,
knowledge_bases=[{
"knowledge_base_id": kb.id,
"top_k": 3,
"rerank_model_id": model_ids["jina-reranker"],
"decider_model_id": model_ids["jev-decider"],
"decider_threshold": 0.5,
}],
)
kept lapland-northern-lights.md rerank 0.182 relevant 97%
dropped lisbon-coast.md rerank -0.131 relevant 11%
dropped kyoto-autumn.md rerank -0.135 relevant 11%

5. Read each stage in the trace​

Click Chat and ask I'd like a winter trip with snow. Which one do you have and what does it cost?, then click View trace under the answer.

The search step holds four spans. Embedding turns the query into a vector. Rerank says top 3 of 3: the reranker ranked 3 candidates and kept the top 3. Decision says kept 1 of 3: the decision model passed one passage on to the agent.

The trace of the run: an LLM step, a numbered Retrieval step, and under it the Embedding, Rerank (top 3 of 3) and Decision (kept 1 of 3) spans

Click the Decision row. It shows the model, the tokens, the cost and the gateway request the call was logged under. Its attributes hold the number kept, the number dropped and the threshold.

The expanded Decision span with the model jev-decider, 736 tokens, its cost, the linked gateway request, and attributes kept 1, dropped 2 and threshold 0.5

Below the attributes, Input holds the query, the user's message, the threshold and each passage with its position. Output has one row per passage: its rating in decision, and kept set to true or false. The Rerank span has the same layout: the candidates in, and the ranking with each rerankScore out. Input and output are stored when payload capture is on, which is the default (payload capture).

The Output of the Decision span: lapland-northern-lights.md kept with decision 0.98, and kyoto-autumn.md not kept with decision 0.05

When a stage fails​

A reranker or decision model that fails never fails the search or the run. The search skips that stage and returns what it had: a failed rerank keeps the order from the vector search, and a failed decision keeps every passage. The span is marked as an error, its row says what the search did instead, and the error message says why the stage failed.

When the decision model drops every passage, the search tool tells the agent that no relevant passage was found, so the model can say so instead of answering from nothing.

Next steps​