Agent Invocation
The client.agents resource provides three invocation methods: invoke (sync), ainvoke (async), and stream (SSE streaming).
Synchronous Invocationβ
from faos import FaosClient
client = FaosClient(api_key="faos_sk_...")
response = client.agents.invoke(
"credit-risk-analyst",
{"query": "Analyze Q1 2024 revenue trends"}
)
print(response.result) # Agent output dict
print(response.metadata) # Execution metadata (model, duration)
print(response.usage) # Token usage breakdown
print(response.request_id) # For debugging/support
Async Invocationβ
import asyncio
from faos import FaosClient
async def main():
client = FaosClient(api_key="faos_sk_...")
response = await client.agents.ainvoke(
"credit-risk-analyst",
{"query": "Analyze Q1 2024"}
)
print(response.result)
asyncio.run(main())
Streamingβ
Stream responses via Server-Sent Events (SSE). Each chunk has a type field:
| Type | Description |
|---|---|
text | Text content from the agent |
tool_call | Agent invoked a tool |
metadata | Execution metadata |
error | Error during execution |
done | Stream complete with final metadata |
import asyncio
from faos import FaosClient
async def stream():
client = FaosClient(api_key="faos_sk_...")
async for chunk in client.agents.stream(
"credit-risk-analyst",
{"query": "Analyze Q1 2024"}
):
if chunk.type == "text":
print(chunk.data, end="", flush=True)
elif chunk.type == "done":
print(f"\nTokens used: {chunk.data}")
asyncio.run(stream())
Reconnectionβ
Pass last_event_id to resume a stream after disconnection:
async for chunk in client.agents.stream(
"analyst", {"query": "..."}, last_event_id="evt-42"
):
...
Multi-Tenant Invocationβ
Pass tenant_id for multi-tenant deployments:
response = client.agents.invoke(
"credit-risk-analyst",
{"query": "Analyze Q1"},
tenant_id="tenant-abc123",
)
A2A Formatβ
The SDK auto-detects A2A (Agent-to-Agent) format when task key is present:
# FAOS-native format
response = client.agents.invoke("analyst", {"query": "..."})
# A2A format β auto-detected
response = client.agents.invoke("analyst", {
"task": {"id": "task-1", "input": {"query": "..."}}
})
Response Modelβ
class FaosResponse(BaseModel):
result: dict # Agent output
metadata: dict # model, duration_ms, etc.
usage: TokenUsage # prompt_tokens, completion_tokens, total_tokens
request_id: str # For debugging