Getting started#

Install#

pip install needtoknow

Python 3.11 or newer: every Python version CPython still supports, each dropped in the first minor release after its end of life. The core has no dependencies; each integration is an optional extra, for example pip install "needtoknow[pgvector]".

Async#

aretrieve takes the same arguments and gives the same guarantees:

import asyncio


async def main():
    result = await retriever.aretrieve("leave salary bands", bob, k=3)
    print([hit.id for hit in result.hits])


asyncio.run(main())

Connect a vector store#

With PostgreSQL and pgvector (pip install "needtoknow[pgvector]"):

from psycopg_pool import ConnectionPool

from needtoknow import Document, MetadataACLAuthorizer, PermissionedRetriever, Subject
from needtoknow.backends.pgvector import PgVectorBackend, create_schema

pool = ConnectionPool("postgresql://localhost/app")
create_schema(pool, dim=768)  # the table, a GIN index on metadata and an HNSW index
backend = PgVectorBackend(pool, embed=embed)  # embed: your model, str -> list[float]
backend.add([Document("salaries", "Salary bands for 2026 ...", {"allowed_groups": ["hr"]})])

retriever = PermissionedRetriever(backend, MetadataACLAuthorizer())
result = retriever.retrieve("What are the salary bands?", Subject("alice", groups=["hr"]))

The permission filter runs inside the SQL query, so the result is the nearest chunks Alice may read, however few of the table’s rows that is.

Where the subject comes from#

Build the Subject from your authenticated session: the user id and groups your identity provider vouches for. Never take them from the request body, and never let a language model choose them, for example as a tool argument. See Security model.

Next#

  • Concepts explains strategies, filters and the guarantees.

  • Integrations covers each vector store, permission system and framework.

  • Operations covers caching, audit logs and tracing.