{"slug": "stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and", "title": "Stop manually logging your AI API calls — one-line auto-logging for OpenAI and Anthropic", "summary": "A developer released AICostTracker, an open-source tool that adds one-line auto-logging for OpenAI and Anthropic API calls. The library's track() function wraps the client to intercept responses and extract token usage, providing per-project cost summaries via CLI without modifying existing code.", "body_md": "Every time you call an AI API, you're spending money. But unless you're checking the dashboard after each session, you have no idea which project or model is driving the bill.\n\nI solved this by wrapping my AI clients with a one-line middleware that logs every call automatically.\n\n[AICostTracker](https://github.com/Ozperium/aicost-tracker) lets you run `aicost log myapp gpt-4o 1500 800`\n\nto record a call. Useful — but nobody does this consistently. You forget, you're in flow, you'll do it later.\n\nThe logs end up patchy and the summary is useless.\n\n`track()`\n\nIn v0.1.2, I added a `track()`\n\nfunction that wraps your OpenAI or Anthropic client:\n\n```\nnpm install @ozperium/aicost-tracker\npython\nimport OpenAI from 'openai';\nimport { track } from '@ozperium/aicost-tracker';\n\nconst openai = track(new OpenAI(), { project: 'myapp' });\n\n// From here, every call is logged automatically\nconst response = await openai.chat.completions.create({\n  model: 'gpt-4o',\n  messages: [{ role: 'user', content: 'Summarize this doc' }]\n});\n```\n\nThat's it. No changes to your existing code beyond the wrap.\n\nIt works the same way with Anthropic:\n\n``` python\nimport Anthropic from '@anthropic-ai/sdk';\nimport { track } from '@ozperium/aicost-tracker';\n\nconst anthropic = track(new Anthropic(), { project: 'summarizer' });\nconst response = await anthropic.messages.create({ model: 'claude-3-5-sonnet-20241022', ... });\n```\n\nAfter a few sessions:\n\n```\naicost summary\nAI Cost Tracker — Summary\n  ══════════════════════════════════════════════════\n  Total cost:       $1.2847\n  Input tokens:     124,000\n  Output tokens:    67,500\n\n  By Project:\n  ──────────────────────────────────────────────────\n  myapp                $    0.9102  31 calls\n  summarizer           $    0.3745  12 calls\n\n  By Model:\n  ──────────────────────────────────────────────────\n  gpt-4o               $    0.9102  31 calls\n  claude-3-5-sonnet    $    0.3745  12 calls\n```\n\n`track()`\n\npatches `client.chat.completions.create`\n\n(OpenAI) or `client.messages.create`\n\n(Anthropic) to intercept the response, extract usage from the `usage`\n\nfield, and call `logUsage()`\n\nbefore returning. It's a thin synchronous wrapper — zero added latency.\n\nIf logging fails for any reason, the error is swallowed silently. Your API call always gets its response.\n\nThe `{ project: 'myapp' }`\n\noption lets you tag every call with a project name. If you're running multiple agents or tools in the same codebase, give each its own project tag:\n\n``` js\nconst researchAgent = track(new OpenAI(), { project: 'research' });\nconst summaryAgent = track(new OpenAI(), { project: 'summary' });\n```\n\nThen `aicost summary`\n\nshows the breakdown by project automatically.\n\nGitHub: [https://github.com/Ozperium/aicost-tracker](https://github.com/Ozperium/aicost-tracker)\n\n*Also: AgentSpec for testing AI agent behavior, quota for monitoring rate limits.*", "url": "https://wpnews.pro/news/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and", "canonical_source": "https://dev.to/pawfromoz/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and-anthropic-1b5m", "published_at": "2026-07-25 04:23:00+00:00", "updated_at": "2026-07-25 04:58:50.451966+00:00", "lang": "en", "topics": ["developer-tools", "ai-infrastructure", "ai-products"], "entities": ["AICostTracker", "OpenAI", "Anthropic", "Ozperium"], "alternates": {"html": "https://wpnews.pro/news/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and", "markdown": "https://wpnews.pro/news/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and.md", "text": "https://wpnews.pro/news/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and.txt", "jsonld": "https://wpnews.pro/news/stop-manually-logging-your-ai-api-calls-one-line-auto-logging-for-openai-and.jsonld"}}