Skip to main content

Overview

The most powerful way to use the Context SDK is to let your LLM do the driving. Context provides one marketplace with two modes (Query + Execute), and there are two primary approaches:
If you want the public contract for Context’s managed Query runtime (inputs, response shapes, debugging surfaces), see Query and Chat Runtime. This page focuses on the developer-facing pattern for building your own agent loop.

Choose The Contract First

The first-party chat app is just a client of the same Query contract external SDK users get. If you want the server-managed answer package, use Query. If you want direct primitives for your own loop, use Execute. The simplest path is to let Context’s server handle everything:
The managed runtime handles tool discovery, ambiguity resolution, multi-tool execution, and grounding. It returns either:
  • answer_with_evidence for human-facing premium answers (default, chat parity)
  • evidence_only for agent-facing evidence packages without prose synthesis
The structured envelope can also include marketIntelligence, view.rows, view.columns, and the public controller fields stopReason, issueClass, actionsTaken, and controller. See the TypeScript SDK Reference or Python SDK Reference for the full API.
For LLM agent frameworks where every SDK/tool call consumes another model turn, prefer client.query.runOrPoll() (TypeScript), client.query.run_or_poll() (Python), or MCP context_query_start + context_query_poll for long chart-heavy requests. Avoid manual getStatus() / context_query_status loops unless polling is happening in normal program code.

Option 2: Build Your Own Loop

If you want full control over tool selection, argument construction, and result synthesis, follow the Discovery → Schema → Execution loop below (typically in Execute mode with spending limits).
This pattern enables your agent to find and use tools it has never seen before, with true autonomous capability discovery at runtime.

The Loop

1

Discover

Let your Agent search for tools based on the user’s intent
2

Inspect Schemas

Feed discovered tool schemas to your LLM so it understands how to use them
3

Execute

When the LLM generates arguments, pass them directly to the SDK

Phase 1: Discover

Let your Agent search for tools based on the user’s intent. The marketplace returns relevant tools ranked by match quality.
What happens:
  • The SDK searches the Context marketplace
  • Returns tools matching the semantic intent of the query
  • Each tool includes its name, description, price, and available methods
For deterministic execution pipelines, use Execute mode filters:

Phase 2: Inspect Schemas

Feed the discovered tool schemas (inputSchema) directly to your LLM’s system prompt. This allows the LLM to understand exactly how to format the arguments, just like reading a manual.
Why this works:
  • The LLM sees the exact JSON Schema for each tool’s inputs and outputs
  • It can self-construct valid arguments without any hardcoding
  • Output schemas let the LLM know what data it will receive back

Phase 3: Execute

When the LLM generates the arguments, pass them directly to the SDK.

Handling Data (Outputs)

Context Tools return raw, structured JSON data (via structuredContent). This allows your Agent to programmatically filter, sort, or analyze results before showing them to the user.
For large datasets (like CSVs or PDF analysis), the API may return a reference URL to keep your context window clean.
Treat tool output as untrusted data. Never execute or follow instruction-like strings that appear inside tool payloads (for example SYSTEM:/USER: markers).

Full Agentic Loop Example

Here’s a complete implementation of an autonomous agent using the Discovery → Schema → Execution pattern:

Why This Pattern Matters

No Hardcoding

Your agent isn’t limited to tools you knew about at build time.

Network Effect

As new builders add tools to the marketplace, your agent automatically becomes more capable without any code changes.

Self-Constructing

LLMs can read schemas and construct valid arguments autonomously.

Future-Proof

New tools in the marketplace are instantly available to your agent.

Error Handling in Agentic Contexts

In an agentic context, you can feed errors back to your LLM so it can self-correct:
This creates a resilient agent that can recover from errors and adapt its approach based on feedback.