See inside every RAG answer.
A RAG answer is an embedding, a search and a generation. Worksi traces each part, prices the embeddings and the prompt it built, and flags context that keeps growing.
- workflow_rundocs-qaone question
one question
- agent_stepretrieve
- llm_callembeddingstext-embedding-3-small · tokens, cost
text-embedding-3-small · tokens, cost
- tool_callsearch_docsdocs-index · top 5
docs-index · top 5
- agent_stepgenerate
- llm_callchat.completionsgpt-5-mini · prompt size, cost
gpt-5-mini · prompt size, cost
An answer that outgrew its context.
A change retrieves more chunks for every question, and every prompt carries them all. The answers read the same; the bill does not.
Example values. Detector rules are the product defaults, and the response is in the gateway’s format.
- 01
40 chunks in one prompt
Retrieval is a tool span with its data source, generation a model call with its prompt size, so the trace shows what one question pulled in.docs-qa · the answer that was flagged - workflow_rundocs-qaone question
one question
- agent_stepretrieve
- llm_callembeddingstext-embedding-3-small · 12 tokens
text-embedding-3-small · 12 tokens
- tool_callsearch_docs · top 40docs-index
docs-index
- agent_stepgenerate
- llm_call64,800 input tokenschat.completions gpt-5-mini
chat.completions gpt-5-mini
- 02
Past both thresholds
token_anomalyfires when a call passes 60,000 tokens, or 4× the agent’s p95. This prompt passed both.Incident · detector - detector
- token_anomaly
- rule
- > 60,000 tokens or > 4× agent p95
- this call
- 65,230 tokens
- agent p95, 7 days
- 12,100 tokens
- 03
Input grows, output doesn’t
Answer by answer, the input grows with the number of chunks while the output stays the same length.Evidence · input tokens per answer - answer 1
- 5 chunks · 9,640 tokens
- answer 2
- 12 chunks · 21,300 tokens
- answer 3
- 25 chunks · 43,900 tokens
- answer 4
- 40 chunks · 64,800 tokens
- 04
A daily token cap
Put a ceiling on it. Atoken_limitpolicy’s daily cap stops the agent once its tokens for the day are spent, before the call reaches your provider; its per‑request cap limits themax_tokensa call may ask for.ResponseHTTP/1.1 403 Forbidden{ "error": { "message": "daily token limit of … for agent reached (used …, policy \"…\")", "type": "permission_error", "code": "token_limit_exceeded", "param": null, "aiw_request_id": "req_…" }}Example response. The body format is the gateway’s.
What else watches retrieval.
Five of the eleven built‑in detectors, with the conditions they fire on.
| Detector | Fires when |
|---|---|
Token anomalytoken_anomaly | A call uses far more tokens than the agent normally does |
Tool failuretool_failure | A tool keeps failing, or a run ends in error at a tool call |
Repeated tool callrepeated_tool_call | The same tool is called with the same arguments again and again in one run |
Timeouttimeout | Model or tool calls time out or run past their thresholds |
Cost anomalycost_anomaly | Cost per request jumps versus the agent’s 7-day baseline, or a run exceeds its cost limit |
Trace retrieval and generation.
Embeddings and chat go through the gateway. With the SDK, the vector search becomes a tool span with its data source, so the AI Graph shows which agents read which index.
Pro fits most RAG products: 1M gateway requests and 10M spans a month, 30‑day retention, the AI Graph. $49 / month.
SDK packages are distributed by Worksi. Request access. Any OpenAI or Anthropic SDK works with the gateway without them.
import osfrom openai import OpenAI client = OpenAI( base_url="https://gateway.worksi.ai/v1", api_key=os.environ["AIW_API_KEY"], default_headers={"X-AIW-Agent": "docs-assistant"},) def answer(question: str) -> str: # embeddings are priced and traced like chat calls query = client.embeddings.create( model="text-embedding-3-small", input=question, ) chunks = search_docs(query.data[0].embedding, top_k=5) reply = client.chat.completions.create( model="gpt-5-mini", messages=build_prompt(question, chunks), ) return reply.choices[0].message.content or ""from aiworkforce import AIW aiw = AIW(service="docs-assistant")client = aiw.openai() def answer(question: str) -> str: with aiw.run("docs-qa", agent="docs-assistant") as run: with aiw.step("retrieve"): query = client.embeddings.create( model="text-embedding-3-small", input=question, ) with aiw.tool("search_docs", args={"top_k": 5}, data_source="docs-index") as t: chunks = search_docs(query.data[0].embedding) t.set_result({"chunks": len(chunks)}) with aiw.step("generate"): reply = client.chat.completions.create( model="gpt-5-mini", messages=build_prompt(question, chunks), ) run.outcome("question_answered", success=True) return reply.choices[0].message.content or ""Trace one question end to end.
Start with the embedding call: through the gateway it is priced and traced like a chat call.
- 3‑day trial with Pro features, no card.
Keep readingObservabilityAI agentsCapture modes
curl -s https://gateway.worksi.ai/v1/embeddings \ -H "Authorization: Bearer $AIW_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "text-embedding-3-small", "input": "refund policy"}'