> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trycaesar.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LlamaIndex

> Use Caesar as LlamaIndex function tools for search-first, read-second retrieval workflows.

Install Caesar with the LlamaIndex package set you already use:

```bash theme={"system"}
pip install caesar-search llama-index
```

## Function tools

```python theme={"system"}
from caesar_search import Caesar
from llama_index.core.tools import FunctionTool

client = Caesar()  # reads CAESAR_API_KEY


def web_search(query: str, max_results: int = 5) -> str:
    """Search the web with Caesar and return compact JSON."""
    response = client.search(query, max_results=max_results, verbosity="compact")
    return response.model_dump_json()


def web_fetch(target: str, query: str | None = None, max_chars: int | None = None) -> str:
    """Read a Caesar doc_id or URL as markdown plus metadata."""
    response = client.read(target, query=query, max_chars=max_chars)
    return response.model_dump_json()


web_search_tool = FunctionTool.from_defaults(
    fn=web_search,
    name="web_search",
    description="Search the web with Caesar. Returns ranked results with doc_id values.",
)

web_fetch_tool = FunctionTool.from_defaults(
    fn=web_fetch,
    name="web_fetch",
    description="Read a Caesar doc_id or URL as clean markdown.",
)

tools = [web_search_tool, web_fetch_tool]
```

Pass `tools` into your agent workflow. Keep the JSON response intact unless you have a reason to trim it; the `doc_id`, `search_id`, and provenance fields are the useful part of the result.

## Search, then read

The strongest pattern is a two-step loop:

1. `web_search(query, max_results=5)` to find candidates.
2. Pick the most relevant `doc_id`.
3. `web_fetch(doc_id, query="what the agent needs")` to fetch clean markdown.
4. Send feedback through the SDK if a result helped.

```python theme={"system"}
results = client.search("postgres 17 logical replication failover", max_results=5)
doc_id = results.results[0].doc_id
doc = client.read(doc_id, query="failover behavior")
client.feedback("result_helpful", search_id=results.search_id, doc_id=doc_id)
```

## For agents

* Return JSON from the tool when possible. It preserves snake\_case fields and prevents accidental loss of `doc_id`.
* Prefer `verbosity="compact"` for the search tool. Read the selected document for longer evidence.
* On truncated reads, continue with the returned `start_char` value instead of asking for a larger `max_chars`.
* If your LlamaIndex runtime can consume MCP tools directly, use the [remote MCP server](/clients/mcp/remote) and skip custom wrappers.
