Skip to main content
Once your Contextual AI Agent is configured and your documents are uploaded, you can begin sending queries to it. Use the /query endpoint to submit messages and retrieve responses programmatically. Be sure to:
  • Replace {agent_id} in the url path with a valid agent_id
  • Replace $API_KEY with your API key
  • Replace the content field with a question that is relevant to the document(s) you uploaded
from contextual import ContextualAI

# Initialize the client with your API key
contextual = ContextualAI(api_key="API_KEY")

# Query the agent
response = contextual.agents.query.create(
  agent_id=agent_id,
  messages=[
    {
      "role": "user",
      "content": "What is the revenue of Apple?"
    }]
)
If the request is successful, you’ll receive a response back containing:
  • The body of the response
  • The sources retrieved from the datastore that are relevant to the response
  • Attributions/citations of sources to specific text spans in the response
You can query your agent only after at least one document in the datastore has been fully processed. Please review the following instructions for verifying your document’s upload status.