@combycode/llm-sdk 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,139 @@ All notable changes to `@combycode/llm-sdk` are documented here. The format foll
4
4
  [Keep a Changelog](https://keepachangelog.com/) and the project adheres to
5
5
  [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [2.2.1] — 2026-08-17
8
+
9
+ ### Fixed
10
+
11
+ - **Extended thinking returned a 400 on Claude 4.7 and later.** The Anthropic adapter sent
12
+ `thinking: {type:'enabled', budget_tokens: N}` to every model, on the reasoning that it was the
13
+ universally accepted shape — true when written, and since reversed. Anthropic removed
14
+ `budget_tokens` on 4.7+, so Sonnet 5, Opus 5/4.8/4.7 and Fable 5 rejected every thinking request
15
+ outright:
16
+
17
+ > `"thinking.type.enabled" is not supported for this model. Use "thinking.type.adaptive"`
18
+
19
+ There is no shape that works everywhere: Haiku 4.5, Sonnet 4.5 and the Opus 4.x line have no
20
+ adaptive mode at all and still require the budget (`adaptive thinking is not supported on this
21
+ model`), so a blanket switch would have broken the other half. The shape is now chosen per model
22
+ at 4.6 — the version that accepts both — with `effort` mapping to `output_config.effort` on the
23
+ adaptive side instead of a token budget. An unrecognised model id gets `adaptive`, since
24
+ `budget_tokens` is the shape being retired. Both halves verified against the live API.
25
+
26
+ - **`complete()` silently dropped `thinking`.** The one-shot helper never declared the option, so a
27
+ reasoning request through the simplest entry point sent no thinking at all while `client.complete()`
28
+ and agents honoured it. Found while live-testing the fix above — the run came back green because
29
+ nothing was being sent.
30
+
31
+ ## [2.2.0] — 2026-08-17
32
+
33
+ ### Added
34
+
35
+ - **Agents can be named: `label`, `source` and an `attributes` bag.** An unlabelled agent
36
+ exported as a bare `invoke_agent` carrying only a per-process id, so a trace could not say
37
+ which agent ran or be compared across runs. `label` becomes `gen_ai.agent.name` and names
38
+ the span (`invoke_agent briefing`); `source` records which part of the host system the
39
+ agent belongs to, as free text because the taxonomy is the application's; `attributes`
40
+ stamps anything else onto the run. Library attributes win on a key collision, so the bag
41
+ cannot rewrite what a span claims to be.
42
+
43
+ - **`onTrace` -- an event surface, so this SDK can be one source in a bigger pipeline.**
44
+ Configure it at `createEngine({ telemetry: { types, content, sample, onTrace } })` and take
45
+ the levels you want: an operator reading business traces does not want our HTTP retries,
46
+ and an SDK that ships its own exporter just competes with the pipeline they already run.
47
+ Events carry `traceId` / `spanId` / `parentSpanId`, so a consumer pushes them straight
48
+ into their own tracer. Nothing is sent anywhere by the library.
49
+
50
+ Filtering **splices** the tree rather than punching holes in it -- drop `http` and its
51
+ children re-parent to the nearest ancestor that subscriber still receives, because a
52
+ dangling parent renders as a second root. Sampling is per **trace**, hashed from the
53
+ trace id so two services sharing one agree without coordinating; sampling per span would
54
+ shred every tree it touched. Conversation content is off by default and rides on
55
+ `message` events only, never on spans, so spans can go to a metrics backend without
56
+ carrying prompts into it.
57
+
58
+ - **The SDK can run inside your application's trace.** Pass `ctx.traceparent` -- the W3C header
59
+ shape -- and every span the run emits joins that trace and hangs under that span instead of
60
+ rooting one of its own. A business chain and the model calls it triggers now arrive as one
61
+ request rather than two unrelated traces. A malformed header is ignored rather than fatal.
62
+
63
+ ### Changed
64
+
65
+ - **Exported spans follow the GenAI semantic conventions.** `agent.run` and `tool.call` export as
66
+ `invoke_agent` and `execute_tool {name}`, carrying `gen_ai.operation.name`, `gen_ai.agent.id`,
67
+ `gen_ai.tool.name` and `gen_ai.tool.call.id`. Names only this library understood forced every
68
+ consumer to write its own mapping; a backend that speaks the conventions now recognises the work
69
+ without one. Internal names are unchanged, so `snapshot()` and the sandbox sidebar group as
70
+ before.
71
+
72
+ **If you read span attributes,** three keys moved: `tool.name` → `gen_ai.tool.name`,
73
+ `agent.id` → `gen_ai.agent.id`, `agent.model` → `gen_ai.request.model`. Span *names* in
74
+ `snapshot()` are untouched; only the exported ones changed.
75
+
76
+ ### Fixed
77
+
78
+ - **Spans had no parent, so a backend drew a flat list instead of a tree.** Every span was a
79
+ sibling, and a turn read as "nine things happened" rather than "a run, which called a tool, which
80
+ asked a second model". Spans now carry `parentSpanId`, resolved to the innermost enclosing
81
+ `agent.run` / `tool.call`, else the caller's span, else none.
82
+
83
+ - **An agent nested inside a tool call orphaned the run around it.** The enclosing span was tracked
84
+ as one slot per trace, so a second run on the same trace overwrote it and then deleted it on
85
+ close, leaving the rest of the outer run parentless. It is a stack now, and a container is removed
86
+ by id rather than popped, because parallel tool calls close out of order.
87
+
88
+ - **`traceparent` was dropped at two layers, each by hand-picking fields off the trace.** `beginRun`
89
+ built `{ sessionId, requestId }` and `LLMClient` handed `{ sessionId, requestId, callId }` to the
90
+ network layer. One request became three traces: the model calls joined the caller's, while
91
+ `agent.run`, every `tool.call`, every nested agent and every `http.request` rooted their own. The
92
+ trace now travels whole, and `RunTrace` replaces a shape written inline at eleven signatures.
93
+
94
+ - **One agent run arrived as several unrelated traces.** The agent built a `runTrace` for its own
95
+ spans and never handed it to the LLM calls it made, so each call fell through to mint-if-absent and
96
+ invented its own `requestId`. Since the trace id is `sessionId:requestId`, a single conversation
97
+ fragmented: measured against a live Grafana Tempo endpoint, one turn with one tool call produced
98
+ SIX traces. Every span looked correct on its own, which is why it survived until telemetry was
99
+ pointed at a real backend.
100
+
101
+ - **A caller's own trace ids were discarded, then half-honoured.** `ctx.sessionId` / `ctx.requestId`
102
+ now win over the agent's, and the run trace is derived in ONE place from them — deriving it
103
+ separately for agent spans and LLM calls meant a caller passing only `sessionId` split the run in
104
+ two. `ctx.conversationId` likewise wins over the history id instead of being silently overwritten.
105
+
106
+ - **`agent.run` and `tool.call` spans used an entity id as the trace id** — the run id and the tool
107
+ call id respectively — putting them in a different trace from the work they describe. One span's
108
+ trace id was literally `t1`. MCP spans keyed their trace by server name, merging every call to a
109
+ server over the process lifetime into one eternal trace.
110
+
111
+ - **Span ids collided once a run shared one trace.** The span KEY (`llm:${traceId}`) doubled as the
112
+ span ID, so every LLM call in a run emitted the same id and the collector merged them into one
113
+ span. Key and id are now separate.
114
+
115
+
116
+
117
+
118
+ - **`toOtlpTraces()` produced JSON that only LOOKED like OTLP, and no collector would accept it.**
119
+ Trace ids went out as `s:r` and span ids as `llm:s:r` where the protocol requires 16- and 8-byte
120
+ hex; `kind` was the string `'llm'` where it must be the int enum; and every attribute value was
121
+ `String(value)`, so `gen_ai.usage.input_tokens` arrived as text and could not be summed by any
122
+ backend. Ids are now derived deterministically from the readable internal ones, so a trace split
123
+ across two exports still joins up.
124
+
125
+ - **LLM spans used attribute names no backend recognises.** `gen_ai.provider` / `gen_ai.model` are
126
+ not in the OTel GenAI semantic conventions; the required names are `gen_ai.provider.name` and
127
+ `gen_ai.operation.name`, with `gen_ai.request.model`. A span carrying the old names is not
128
+ identified as a model call at all. Adds `gen_ai.response.model` (the model that actually answered,
129
+ which an alias can change) and `gen_ai.conversation.id` (the agent's history id).
130
+
131
+ - **Point spans could share an id, and the backend silently dropped the duplicates.**
132
+ `mcp:connect:${server}` repeated on every reconnect and `media:${traceId}` repeated for a second
133
+ image in the same run; `mcp:tool:…:${Date.now()}` collided for two calls in one millisecond. A
134
+ duplicate span id within a trace is invalid OTLP, so those runs looked like they did less work
135
+ than they did.
136
+
137
+ The in-memory model is unchanged — `snapshot()` still returns readable ids and the domain `kind`,
138
+ which is what the sandbox groups by. Only the export is translated.
139
+
7
140
  ## [2.1.0] — 2026-08-17
8
141
 
9
142
  Minor, not major: everything below is additive or a bug fix, and no export was removed or
@@ -14,6 +14,25 @@ import type { Persistence } from '../plugins/persistence/types';
14
14
  export interface AgentLoopConfig {
15
15
  /** LLM client. AgentLoop reads `client.model` and uses `client.complete`/`client.stream`. */
16
16
  client: LLMClient;
17
+ /** Human name for this agent, e.g. `'briefing'`. Without it telemetry only has the
18
+ * agent's generated id, and a trace reads as `invoke_agent` with no clue which of your
19
+ * agents ran — the ids differ per process, so they cannot be compared across runs
20
+ * either. With it the span becomes `invoke_agent briefing` and carries
21
+ * `gen_ai.agent.name`, which is what the conventions ask for. */
22
+ label?: string;
23
+ /** Which part of YOUR system this agent belongs to, e.g. `'customer'`, `'moderation'`.
24
+ *
25
+ * Free text rather than a fixed set, because the taxonomy is the application's: a
26
+ * library cannot know whether you divide by product surface, team, or bounded context,
27
+ * and forcing our categories on you would only make you encode yours inside a `label`.
28
+ * Exported as `agent.source` — our attribute, not a convention one; the GenAI spec has
29
+ * no term for it. */
30
+ source?: string;
31
+ /** Extra attributes stamped on this agent's span, for whatever the fixed fields do not
32
+ * cover — tenant, tier, experiment arm. Keys are used verbatim, so namespace them
33
+ * (`app.tenant`) to stay clear of convention attributes; ours win on a collision, so a
34
+ * stray key here cannot corrupt `gen_ai.*`. */
35
+ attributes?: Record<string, string | number | boolean>;
17
36
  /** Persona / role text for the agent. Stored as the `agentloop.system` registry
18
37
  * layer (priority 10). Composed with other system-tagged layers when sending.
19
38
  * When passed as a function, it is re-evaluated at the start of every
@@ -7,6 +7,19 @@ import type { StreamEvent } from '../llm/types/stream';
7
7
  import type { TraceContext } from '../network/types';
8
8
  import type { AgentStreamEvent, AgentTool, ToolCallReport, ToolExecutionContext } from './types';
9
9
  import type { StepState } from './loop-step-state';
10
+ /** The trace one agent run belongs to — resolved once in `beginRun` and handed to
11
+ * everything the run emits: its own span, its LLM calls, its tool calls, and any agent
12
+ * nested inside a tool.
13
+ *
14
+ * Not a bare `TraceContext` because `sessionId`/`requestId` are always resolved here
15
+ * (mint-if-absent), while `traceparent` appears only when the caller runs us inside a
16
+ * span of its own. Named rather than written inline because the shape was repeated at
17
+ * eleven signatures — and adding a field to ten of them is how a run ends up split
18
+ * across two traces. */
19
+ export type RunTrace = TraceContext & {
20
+ sessionId: string;
21
+ requestId: string;
22
+ };
10
23
  /** Create a fresh StepState for the start of a streaming step. */
11
24
  export declare function makeStepState(): StepState;
12
25
  /** Accumulate one SSE StreamEvent into StepState.
@@ -23,6 +23,12 @@ import type { PermissionPolicy } from '../plugins/permissions/policy';
23
23
  import type { ApprovalRequest, ApprovalDecision, PendingToolCall } from './approval-types';
24
24
  export declare class AgentLoop {
25
25
  readonly id: string;
26
+ /** Human name, surfaced as `gen_ai.agent.name` — see AgentLoopConfig.label. */
27
+ readonly label?: string;
28
+ /** Which part of the host system this agent belongs to. */
29
+ readonly source?: string;
30
+ /** Extra attributes stamped on this agent's spans. */
31
+ readonly attributes?: Record<string, string | number | boolean>;
26
32
  readonly client: LLMClient;
27
33
  readonly hooks: HookBus;
28
34
  private _system;
@@ -226,6 +226,12 @@ export interface AgentDestroyContext {
226
226
  export interface RunStartContext {
227
227
  runId: string;
228
228
  agentId: string;
229
+ /** Human name for the agent, when one was configured. */
230
+ label?: string;
231
+ /** Which part of the host system the agent belongs to. */
232
+ source?: string;
233
+ /** Extra attributes the host stamped on this agent. */
234
+ attributes?: Record<string, string | number | boolean>;
229
235
  userMessage: string | ContentPart[] | Message[];
230
236
  model: string;
231
237
  system?: string;
@@ -24,6 +24,7 @@ import type { EngineConnect, EngineFetch, EngineFetchStream, FetchFn } from '../
24
24
  import { Cache } from '../plugins/cache/cache';
25
25
  import { CostCollector } from '../plugins/cost-collector/collector';
26
26
  import { ModelCatalog } from '../plugins/model-catalog/catalog';
27
+ import { TelemetryAdapter, type TelemetryAdapterOptions } from '../plugins/telemetry/telemetry';
27
28
  import type { Persistence } from '../plugins/persistence/types';
28
29
  export interface EngineHandle {
29
30
  /** Trace session id — minted once for this engine (the holder), shared by
@@ -54,6 +55,10 @@ export interface EngineHandle {
54
55
  /** CostCollector — subscribes to onCompletion + onMediaGenerated and
55
56
  * prices via catalog. Call `engine.cost.total()` for a running tally. */
56
57
  cost: CostCollector;
58
+ /** TelemetryAdapter — present only when `telemetry` was configured, because an
59
+ * unwanted one would sit there accumulating spans for a process that never reads
60
+ * them. Subscribe with `engine.telemetry.onTrace(...)`, or use it directly. */
61
+ telemetry: TelemetryAdapter | null;
57
62
  /** API keys per provider. Helpers (createLLM, createAgent,
58
63
  * createMediaOutput, complete) read these to wire LLM clients without
59
64
  * the caller passing apiKey explicitly. */
@@ -95,6 +100,20 @@ export interface EngineConfig {
95
100
  /** Per-provider API keys. Helpers consult this when no apiKey is passed
96
101
  * alongside `model: 'provider/...'`. */
97
102
  apiKeys?: Partial<Record<ProviderName, string>>;
103
+ /** Observability. Omitted → no adapter is built and nothing is collected.
104
+ *
105
+ * ```ts
106
+ * createEngine({
107
+ * telemetry: {
108
+ * types: ['agent', 'tool'], // http/llm detail stays out
109
+ * content: 'none', // conversation text off by default
110
+ * sample: 0.05, // per trace, not per span
111
+ * onTrace: (e) => myPipeline.push(e),
112
+ * },
113
+ * });
114
+ * ```
115
+ */
116
+ telemetry?: TelemetryAdapterOptions;
98
117
  /** Retry policy for every request this engine makes.
99
118
  *
100
119
  * Retry is a cross-cutting concern, so it is configured once here rather than threaded through
@@ -15,7 +15,7 @@
15
15
  * LLMClient.complete. Either way the helper destroys its created client
16
16
  * before returning so callers don't leak. */
17
17
  import type { AgentTool } from '../agent/types';
18
- import type { CacheConfig } from '../llm/types/request';
18
+ import type { CacheConfig, ThinkingConfig } from '../llm/types/request';
19
19
  import type { LLMClientConfig } from '../llm/client-config';
20
20
  import type { AudioOptions } from '../llm/types/audio';
21
21
  import type { ContentPart, Message } from '../llm/types/messages';
@@ -67,6 +67,10 @@ export interface CompleteOptions {
67
67
  /** Which output modalities to return. Default ['text']; add 'audio' for a spoken
68
68
  * reply (surfaced as a media part on `response.media`). */
69
69
  outputModalities?: Array<'text' | 'audio'>;
70
+ /** Extended thinking. Missing from this helper until 2.2.1: `client.complete()` and
71
+ * agents honoured `thinking` while a one-shot silently dropped it, so the simplest
72
+ * entry point was the only one that could not reason. */
73
+ thinking?: ThinkingConfig;
70
74
  /** Service tier for this call. Also settable as a `model:tier` suffix (e.g.
71
75
  * `anthropic/claude-opus-4.8:priority`); an explicit value here wins. */
72
76
  serviceTier?: ServiceTier;