SDK Reference¶
Complete API reference for the Coalex SDKs:
- Python SDK (
coalexv1.9.0) -- published on PyPI - TypeScript SDK (
@coalex-ai/sdkv1.7.0) -- published on npm
Installation¶
See Installation for all extras and framework-specific options.
Usage Pattern¶
Every Coalex integration follows the same pattern:
graph LR
A[register] --> B[auto_instrument]
B --> B2[declare_agent]
B2 --> C[coalex_context]
C --> D[evaluate]
D --> E[resolve]
1. Register -- connect to Coalex¶
Call once at application startup. Configures the global TracerProvider with an OTLP exporter pointing at your Coalex proxy.
Endpoint is optional
The SDK defaults to the Azure-hosted cloud (traces.azure.coalex.ai). Only set endpoint for on-prem, GCP deployments, or local development.
2. Auto-instrument -- patch LLM frameworks¶
Automatically patches all installed LLM libraries (OpenAI, LangChain, LlamaIndex, etc.) to emit OpenInference spans.
3. Wrap with context -- tag every span¶
All spans created inside a coalex_context block inherit the agent metadata, making them queryable in the dashboard.
4. Declare agent (optional) -- register before traces¶
Pre-register your agent so the dashboard recognizes it before traces arrive. This step is optional but recommended for a better dashboard experience.
5. Evaluate -- assess risk¶
Submit agent output for automated risk assessment. Low-risk outputs are auto-approved; high-risk ones are escalated for human review.
6. Resolve -- human-in-the-loop¶
When an output is escalated, a human reviewer approves, rejects, or corrects it.
Public API¶
Core Functions¶
| Function | Description | Reference |
|---|---|---|
register() |
Configure the OTLP exporter and global TracerProvider | Details |
declare_agent() |
Pre-register an agent before traces arrive | Details |
coalex_context() |
Create a parent span with agent metadata | Details |
auto_instrument() |
Patch all installed LLM frameworks | Details |
get_prompt() |
Fetch prompt templates from Prompt Vault (Python) | Details |
evaluate() |
Submit output for risk-based evaluation | Details |
resolve() |
Submit human review for an escalation | Details |
Data Classes¶
| Class | Description | Reference |
|---|---|---|
AgentDeclaration |
Result of declare_agent() |
Details |
PromptVersion |
Result of get_prompt() |
Details |
EvaluationDecision |
Result of evaluate() |
Details |
ResolutionResult |
Result of resolve() |
Details |
MetricResult |
Per-field metric scores | Details |
Extension Decorators / Wrappers¶
Instrument custom pipeline steps without LlamaIndex or LangChain. See Extensions.
| Python Decorator | TypeScript Wrapper | Span Kind | Reference |
|---|---|---|---|
@retrieval_span |
retrievalSpan() |
RETRIEVER |
Details |
@embedding_span |
embeddingSpan() |
EMBEDDING |
Details |
@reranker_span |
rerankerSpan() |
RERANKER |
Details |
@tool_span |
toolSpan() |
TOOL |
Details |
@guardrail_span |
guardrailSpan() |
GUARDRAIL |
Details |
Full Example¶
import os
import coalex
from coalex.ext.retrieval import retrieval_span, Document
from openai import OpenAI
# 1. Register
coalex.register(
api_key=os.environ["COALEX_API_KEY"],
service_name="medical-agent",
)
# 2. Auto-instrument
coalex.auto_instrument()
# 3. Declare agent (recommended)
coalex.declare_agent(agent_id="medical-bot", display_name="Medical Bot")
# 4. Custom retrieval step
@retrieval_span(name="knowledge_base", query_arg="query")
def retrieve(query: str) -> list[Document]:
# Your retrieval logic here
return [Document(content="...", id="doc-1", score=0.95)]
# 5. Agent invocation
client = OpenAI()
with coalex.coalex_context(agent_id="medical-bot", request_id="req-456"):
docs = retrieve(query="What are the side effects?")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": f"Based on: {docs[0].content}"}],
)
answer = response.choices[0].message.content
# 6. Evaluate
decision = coalex.evaluate(
request_id="req-456",
input={"question": "What are the side effects?"},
output={"answer": answer},
metrics={"answer": ["f1", "semantic_similarity"]},
)
# 7. Resolve (if escalated)
if decision.status == "escalated":
result = coalex.resolve(
escalation_id=decision.escalation_id,
decision="approved",
reviewer={"name": "Dr. Smith", "email": "dr.smith@hospital.org"},
)
import { register, coalexContext, autoInstrument, declareAgent, evaluate, resolve } from "@coalex-ai/sdk";
import { retrievalSpan, type Document } from "@coalex-ai/sdk/ext";
import OpenAI from "openai";
// 1. Register
register({
apiKey: process.env.COALEX_API_KEY!,
serviceName: "medical-agent",
});
// 2. Auto-instrument
autoInstrument();
// 3. Declare agent (recommended)
await declareAgent({ agentId: "medical-bot", displayName: "Medical Bot" });
// 4. Custom retrieval step
const retrieve = retrievalSpan(
{ name: "knowledge_base" },
async (query: string): Promise<Document[]> => {
// Your retrieval logic here
return [{ content: "...", id: "doc-1", score: 0.95 }];
},
);
// 5. Agent invocation
const client = new OpenAI();
await coalexContext({ agentId: "medical-bot", requestId: "req-456" }, async () => {
const docs = await retrieve("What are the side effects?");
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: `Based on: ${docs[0].content}` }],
});
const answer = response.choices[0].message.content;
// 6. Evaluate
const decision = await evaluate({
requestId: "req-456",
input: { question: "What are the side effects?" },
output: { answer },
metrics: { answer: ["f1", "semantic_similarity"] },
});
// 7. Resolve (if escalated)
if (decision.status === "escalated") {
const result = await resolve({
escalationId: decision.escalationId!,
decision: "approved",
reviewer: { name: "Dr. Smith", email: "dr.smith@hospital.org" },
});
}
});
API Reference¶
coalex ¶
Coalex Python SDK — AI governance observability.
Classes¶
AgentDeclaration
dataclass
¶
EvaluationDecision
dataclass
¶
Result of an evaluate() call.
Source code in coalex/evaluate.py
PromptVersion
dataclass
¶
A single prompt version returned by the Prompt Vault.
Source code in coalex/prompts.py
ResolutionResult
dataclass
¶
Result of a resolve() call.
Source code in coalex/resolve.py
Functions¶
declare_agent ¶
declare_agent(
*,
agent_id: str,
display_name: str | None = None,
metadata: dict | None = None,
_config: CoalexConfig | None = None,
_api_key: str | None = None,
) -> AgentDeclaration
Register or declare an agent before traces arrive.
This is the building block for lazy agent registration. When COA-264's set_prompt() is implemented, it will call this internally to ensure the agent exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Unique agent identifier (the merge key). |
required |
display_name
|
str | None
|
Optional human-friendly name. |
None
|
metadata
|
dict | None
|
Optional metadata (accepted but not persisted yet). |
None
|
_config
|
CoalexConfig | None
|
Override config (testing only). |
None
|
_api_key
|
str | None
|
Override API key (testing only). |
None
|
Source code in coalex/agents.py
instrument_anthropic ¶
instrument_bedrock ¶
instrument_google_genai ¶
Instrument Google GenAI (google-genai SDK) specifically.
instrument_langchain ¶
instrument_llamaindex ¶
instrument_openai ¶
instrument_vertexai ¶
coalex_context ¶
coalex_context(
*,
agent_id: str,
request_id: str | None = None,
version: str | None = None,
) -> Generator[None, None, None]
Create a parent span with Coalex attributes for all child spans.
All spans created inside this context are descendants of a
coalex.invocation span that carries the agent metadata.
This makes every child span queryable by agent_id, request_id,
and version in the Bronze layer.
Context vars are set before the span is created so that
CoalexAttributePropagator can propagate attributes even
across trace boundaries (e.g. when LangChain auto-instrumentors
create new root spans with separate trace IDs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Identifier for the AI agent. |
required |
request_id
|
str | None
|
Unique request/invocation ID. |
None
|
version
|
str | None
|
Agent version string. |
None
|
Source code in coalex/context.py
get_prompt ¶
get_prompt(
*,
agent: str,
name: str,
version: str = "production",
_config: CoalexConfig | None = None,
_api_key: str | None = None,
) -> PromptVersion
Fetch a prompt template from the Prompt Vault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
str
|
Agent ID (e.g., "sales-copilot"). |
required |
name
|
str
|
Prompt name (e.g., "system"). |
required |
version
|
str
|
"production" (default), "staging", "draft", "latest", or integer. |
'production'
|
_config
|
CoalexConfig | None
|
Override config (testing only). |
None
|
_api_key
|
str | None
|
Override API key (testing only). |
None
|
Source code in coalex/prompts.py
register ¶
register(
*,
endpoint: str = "https://traces.azure.coalex.ai",
api_key: str = "dev",
service_name: str = "coalex-sdk-python",
filter_non_ai_spans: bool = False,
suppress_export_errors: bool = True,
) -> None
Configure the Coalex OTLP exporter.
Call once at application startup. Sets the global TracerProvider with a BatchSpanProcessor exporting OTLP/HTTP to the Coalex proxy.
The SDK always sends through the proxy (via LB) so that auth validation is in the path. Never send directly to the collector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Coalex proxy HTTP endpoint (default: cloud). |
'https://traces.azure.coalex.ai'
|
api_key
|
str
|
API key for proxy authentication (default: "dev" for local). |
'dev'
|
service_name
|
str
|
Service name for OTLP resource (default: "coalex-sdk-python"). |
'coalex-sdk-python'
|
filter_non_ai_spans
|
bool
|
If True, only export AI/LLM spans (default: False). |
False
|
suppress_export_errors
|
bool
|
If True, suppress export errors gracefully (default: True). |
True
|