The open-source agent harness - the runtime layer that turns an LLM into a working agent.
Map TFY Gateway costInUSD into completion usage (#206)
Bhavesh Patelc9283214ffcc
diff --git a/packages/harness/src/core/llm/VercelAILLM.ts b/packages/harness/src/core/llm/VercelAILLM.tsindex 60f026f31..bd7740d40 100644--- a/packages/harness/src/core/llm/VercelAILLM.ts+++ b/packages/harness/src/core/llm/VercelAILLM.ts@@ -945,9 +945,12 @@ export function normalizeUsage(usage: {reasoningTokens: number | undefined;textTokens: number | undefined;};+ // LanguageModelUsage.raw: AI SDK has no cost field; extras like gateway costInUSD live here.+ raw?: JSONObject;}): CompletionUsage {const input = usage.inputTokens ?? 0;const output = usage.outputTokens ?? 0;+ const costInUsd = usage.raw?.['costInUSD'];return {input_tokens: input,output_tokens: output,@@ -955,6 +958,7 @@ export function normalizeUsage(usage: {cache_read_tokens: usage.inputTokenDetails.cacheReadTokens ?? undefined,cache_write_tokens: usage.inputTokenDetails.cacheWriteTokens ?? undefined,reasoning_tokens: usage.outputTokenDetails.reasoningTokens ?? undefined,+ cost_in_usd: typeof costInUsd === 'number' && costInUsd >= 0 ? costInUsd : undefined,};}diff --git a/packages/harness/tests/core/llm/VercelAILLM.stream.test.ts b/packages/harness/tests/core/llm/VercelAILLM.stream.test.tsindex c1ffe08c9..8b7e6dfb4 100644--- a/packages/harness/tests/core/llm/VercelAILLM.stream.test.ts+++ b/packages/harness/tests/core/llm/VercelAILLM.stream.test.ts@@ -107,6 +107,19 @@ describe('normalizeUsage', () => {expect(result.cache_read_tokens).toBeUndefined();expect(result.cache_write_tokens).toBeUndefined();expect(result.reasoning_tokens).toBeUndefined();+ expect(result.cost_in_usd).toBeUndefined();+ });++ it('maps gateway costInUSD from usage.raw onto cost_in_usd', () => {+ const usage = makeUsage(10, 5);+ usage.raw = { costInUSD: 0.0042 };+ expect(normalizeUsage(usage).cost_in_usd).toBe(0.0042);+ });++ it('omits cost_in_usd when raw.costInUSD is not a nonnegative number', () => {+ const usage = makeUsage();+ usage.raw = { costInUSD: -0.01 };+ expect(normalizeUsage(usage).cost_in_usd).toBeUndefined();});});@@ -544,14 +557,21 @@ describe('mapStreamToChunks', () => {it('emits a finish chunk with usage and sets the final message finish_reason', async () => {const usage = makeUsage(20, 10);+ usage.raw = { costInUSD: 0.12 };const { chunks, final } = await drainStream(mapStreamToChunks({ stream: makeStream([makeFinishStep('length', usage)]), chunkMeta: CHUNK_META }),);const finishChunk = chunks.find(c => c.choices[0]?.finish_reason !== null);expect(finishChunk?.choices[0]?.finish_reason).toBe('length');- expect(finishChunk?.usage).toMatchObject({ input_tokens: 20, output_tokens: 10, total_tokens: 30 });+ expect(finishChunk?.usage).toMatchObject({+ input_tokens: 20,+ output_tokens: 10,+ total_tokens: 30,+ cost_in_usd: 0.12,+ });expect(final.finish_reason).toBe('length');+ expect(final.usage.cost_in_usd).toBe(0.12);});it('rejects with the provider message on an error part', async () => {