Hacker News new | ask | show | jobs
by bkryza 250 days ago
They have an interesting regex for detecting negative sentiment in users prompt which is then logged (explicit content): https://github.com/chatgptprojects/claude-code/blob/642c7f94...

I guess these words are to be avoided...

33 comments

An LLM company using regexes for sentiment analysis? That's like a truck company using horses to transport parts. Weird choice.
The difference in response time - especially versus a regex running locally - is really difficult to express to someone who hasn't made much use of LLM calls in their natural language projects.

Someone said 10,000x slower, but that's off - in my experience - by about four orders of magnitude. And that's average, it gets much worse.

Now personally I would have maybe made a call through a "traditional" ML widget (scikit, numpy, spaCy, fastText, sentence-transformer, etc) but - for me anyway - that whole entire stack is Python. Transpiling all that to TS might be a maintenance burden I don't particularly feel like taking on. And on client facing code I'm not really sure it's even possible.

So, think of it as a business man: You don't really care if your customers swear or whatever, but you know that it'll generate bad headlines. So you gotta do something. Just like a door lock isn't designed for a master criminal, you don't need to design your filter for some master swearer; no, you design it good enough that it gives the impression that further tries are futile.

So yeah, you do what's less intesive to the cpu, but also, you do what's enough to prevent the majority of the concerns where a screenshot or log ends up showing blatant "unmoral" behavior.

This door lock doesn’t even work against people speaking French, so I think they could have tried a mite harder.
The up-side of the US market is (almost) everyone there speaks English. The down side is, that includes all the well-networked pearl-clutchers. Europe (including France) will have the same people, but it's harder to coordinate a network of pearl-clutching between some saying "Il faut protéger nos enfants de cette vulgarité!" and others saying "Η τηλεόραση και τα μέσα ενημέρωσης διαστρεβλώνουν τις αξίες μας!" even when they care about the exact same media.

For headlines, that's enough.

For what's behind the pearl-clutching, for what leads to the headlines pandering to them being worth writing, I agree with everyone else on this thread saying a simple word list is weird and probably pointless. Not just for false-negatives, but also false-positives: the Latin influence on many European languages leads to one very big politically-incorrect-in-the-USA problem for all the EU products talking about anything "black" (which includes what's printed on some brands of dark chocolate, one of which I saw in Hungary even though Hungarian isn't a Latin language but an Ugric language and only takes influences from Latin).

I just went through quite an adventure trying to translate back and forth from/to Hungarian to/from different languages to figure out which Hungarian word you meant, and arrived at the conclusion that this language is encrypted against human comprehension.
En toute honnêteté, je pense avoir dit "damn it" plus d'une fois à chat gépété avant de fermer la fenêtre dans un accès de rage
Nom de dieu de putain de bordel de merde de saloperie de connard d'enculé de ta mère.
It's like wiping your arse with silk.
There are only Americans on the internet.
Yea.. but.. in English only.

Fortunately I can swear pretty well in Spanish.

Only a native speaker can tell if you swear well in a foreign language.
And Claude can't tell at all.
That's like saying you can use a chisel for woodworking.
If it’s good enough it’s good enough, but just like there are many more options than going full blown LLM or just use a regex there are more options than transpile a massive Python stack to TS or give up.
> Someone said 10,000x slower, but that's off - in my experience - by about four orders of magnitude.

You do know that 10,000x _is_ four orders of magnitude, right? :-D

OP is saying that in their experience it is more like eight orders of magnitude
I guess I need reading glasses ... :-D
They're sending it to an llm anyway tho? Not sure why they wouldn't just add a sentiment field to the requested response shape.
because a regex on the client is free vs gpu compute is absolutely not.
BUT THEY'RE ALREADY RUNNING IT THROUGH THE LLM.
Because they want it to be executed quickly and cheaply without blocking the workflow? Doesn’t seem very weird to me at all.
They probably have statistics on it and saw that certain phrases happen over and over so why waste compute on inference.
More likely their LLM Agent just produced that regex and they didn't even notice.
The problem with regex is multi-language support and how big the regex will bloat if you to support even 10 languages.
Supporting 10 different languages in regex is a drop in the ocean. The regex can be generated programmatically and you can compress regexes easily. We used to have a compressed regex that could match any placename or street name in the UK in a few MB of RAM. It was silly quick.
I think it will depend on the language. There are a few non-latin languages where a simple word search likely won't be enough for a regex to properly apply.
woah. This is a regex use I've never heard of. I'd absolutely love to see a writeup on this approach - how its done and when it's useful.
We're talking about Claude Code. If you're coding and not writing or thinking in English, the agents and people reading that code will have bigger problems than a regexp missing a swear word :).
I talk to it in non-English. But have rules to have everything in code and documentation in english. Only speaking with me should use my native language. Why would that be a problem?
In my experience agents tend to (counterintuitively) perform better when the business language is not English / does not match the code's language. I'm assuming the increased attention mitigates the higher "cognitive" load.
Claude handles human languages other than English just fine.
They only need to look at one language to get a statistically meaningful picture into common flaws with their model(s) or application.

If they want to drill down to flaws that only affect a particular language, then they could add a regex for that as well/instead.

Did you just complain about bloat, in anything using npm?
Why do you need to do it at the client side? You are leaking so much information on the client side. And considering the speed of Claude code, if you really want to do on the client side, a few seconds won't be a big deal.
Depends what its used by, if I recall theres an `/insights` command/skill built in whatever you want to call it that generates a HTML file. I believe it gives you stats on when you're frustrated with it and (useless) suggestions on how to "use claude better".

Additionally after looking at the source it looks like a lot of Anthropics own internal test tooling/debug (ie. stuff stripped out at build time) is in this source mapping. Theres one part that prompts their own users (or whatever) to use a report issue command whenever frustration is detected. It's possible its using it for this.

> a few seconds won't be a big deal

it is not that slow

It looks like it's just for logging, why does it need to block?
Better question - why would you call an LLM (expensive in compute terms) for something that a regex can do (cheap in compute terms)

Regex is going to be something like 10,000 times quicker than the quickest LLM call, multiply that by billions of prompts

This is assuming the regex is doing a good job. It is not. Also you can embed a very tiny model if you really want to flag as many negatives as possible (I don't know anthropic's goal with this) - it would be quick and free.
I think it's a very reasonable tradeoff, getting 99% of true positives at the fraction of cost (both runtime and engineering).

Besides, they probably do a separate analysis on server side either way, so they can check a true positive to false positive ratio.

Oh it’s worse than that. This one ended up getting my account banned: https://github.com/anthropics/claude-code/issues/22284
This is a tricky problem, I mean, Pinyin also uses the English alphabet.
It is not a tricky problem because it has a simple and obvious solution: do not filter or block usage just because the input includes a word like "gun".
Wow, that's horrible.
... and closed for inactivity like basically every issue in the repo, of course.
Because they actually want it to work 100% of the time and cost nothing.
Maybe hard to believe but not everyone is speaking English to Claude
Then they made it wrong. For example, "What the actual fuck?" is not getting flagged, neither is "What the *fuck*".
It is exceedingly obvious that the goal here is to catch at least 75-80% of negative sentiment and not to be exhaustive and pedantic and think of every possible way someone could express themselves.
Classic over-engineering. Their approach is just fine 90% of the time for the use case it’s intended for.
75-80% [1], 90%, 99% [2]. In other words, no one has any idea.

I doubt it's anywhere that high because even if you don't write anything fancy and simply capitalize the first word like you'd normally do at the beginning of a sentence, the regex won't flag it.

Anyway, I don't really care, might just as well be 99.99%. This is not a hill I'm going to die on :P

[1]: https://news.ycombinator.com/item?id=47587286

[2]: https://news.ycombinator.com/item?id=47586932

It compares to lowercase input, so doesn't matter. The rest is still valid
Except that it's a list of English keywords. Swearing at the computer is the one thing I'll hear devs switch back to their native language for constantly
They evidently ran a statistical analysis and determined that virtually no one uses those phrases as a quick retort to a model's unsatisfying answer... so they don't need to optimize for them.
what you are suggesting would be like a truck company using trucks to move things within the truck
That’s what they do. Ever heard of a hand truck?
I never knew the name of that device.

Thanks

Depending on the region you live in, it's also frequently called a "dolly"
Isn’t a dolly a flat 4 wheeled platform thingy? A hand truck is the two wheeled thing that tilts back.
Do we have a hand llm perchance?
Yeah it’s called a regex. With a lot of human assistance it can do less but fits in smaller spaces and doesn’t break down.
It’s also deterministic, unlike llms…
Well, regex doesn't hallucinate....right?
I just went to expertSexChange.com…
buttbuttination
Good to have more than a hammer in your toolbox!
Because they are engineers? The difference between an engineer and a hobbyist is an engineer has to optimize the cost.

As they say: any idiot can build a bridge that stands, only an engineer can build a bridge that barely stands.

Cloud hosted call centers using LLMs is one of my specialties. While I use an LLM for more nuanced sentiment analysis, I definitely use a list of keywords as a first level filter.
A lot if things dont make sense until you involve scale. Regex could be good enough do give a general gist.
Asking a non deterministic software to act like a deterministic one (regex) can be a significantly higher use of tokens/compute for no benefit.

Some things will be much better with inference, others won’t be.

Using some ML to derive a sentiment regex seems like a good actually?
It's more like a truck company using people to transport some parts. I could be wrong here, but I bet this happens in Volvo's fabrics a lot.
because impact of WTF might be lost in the result of the analysis if you solely rely on LLM.

parsing WTF with regex also signifies the impact and reduces the noise in metrics

"determinism > non-determinism" when you are analysing the sentiment, why not make some things more deterministic.

Cool thing about this solution, is that you can evaluate LLM sentiment accuracy against regex based approach and analyse discrepancies

This just proves its vibe coded because LLMs love writing solutions like that. I probably have a hundred examples just like it in my history.
Actually, this could be a case where its useful. Even it only catches half the complaints, that's still a lot of data, far more than ordinary telemetry used to collect.
Don’t worry, they used an llm to generate the regex.
It's more like workers on a large oil tanker using bicycles to move around it, rather than trying to use another oil tanker.
I used regexes in a similar way but my implementation was vibecoded, hmmm, using your analysis Claude Code writes code by hand.
Not everything done by claude-code is decided by LLM. They need the wrapper to be deterministic (or one-time generated) code?
The amount of trust and safety work that depends on google translate and the humble regex, beggars the imagination.
> That's like a truck company using horses to transport parts. Weird choice.

Easy way to claim more “horse power.”

More like a car company transporting their shipments by truck. It's more efficient
LLMs cost money, regular expressions are free. It really isn't so strange.
As far as I can tell they do nothing with it. They just log it.
They had the problem of sentiment analysis. They use regexes.

You know the drill.

LLMs are good at writing complex regex, from my experience
Using regex with LLMs isn't uncommon at all.
Maybe. Could just be a pre filter.
They're searching for multiple substrings in a single pass, regexes are the optimal solution for that.
The issue isn't that regex are a solution to find a substring. The issue is that you shouldn't be looking for substrings in the first place.

This has buttbuttin energy. Welcome to the 80s I guess.

> The issue is that you shouldn't be looking for substrings in the first place.

Why? They clearly just want to log conversations that are likely to display extreme user frustration with minimal overhead. They could do a full-blown NLP-driven sentiment analysis on every prompt but I reckon it would not be as cost-effective as this.

>Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

The only time to use a regex is when searching with a human in the loop. All other uses are better handled some other way.

>They could do a full-blown NLP-driven sentiment analysis on every prompt but I reckon it would not be as cost-effective as this.

Every conversation is sent to an llm at least a thousand times the size of gpt2 which could one shot this nearly a decade ago.

> Every conversation is sent to an llm at least a thousand times the size of gpt2 which could one shot this nearly a decade ago.

Yes, but that is _what the product does_. What we are talking about is _telemetry_.

Very likely vibe coded.

I've seen Claude Code went with a regex approach for a similar sentiment-related task.

My understanding of vibe coding is when someone doesn’t look at the code and just uses prompts until the app “looks and acts” correct.

I doubt you are making regex and not looking at it, even if it was AI generated.

Clbuttic!
It's fast, but it'll miss a ton of cases. This feels like it would be better served by a prompt instruction, or an additional tiny neural network.

And some of the entries are too short and will create false positives. It'll match the word "offset" ("ffs"), for example. EDIT: no it won't, I missed the \b. Still sounds weird to me.

It’s fast and it matches 80% of the cases. There’s no point in overengineering it.
> There’s no point in overengineering it.

I swear this whole thread about regexes is just fake rage at something, and I bet it'd be reversed had they used something heavier (omg, look they're using an LLM call where a simple regex would have worked, lul)...

The pattern only matches if both ends are word boundaries. So "diffs" won't match, but "Oh, ffs!" will. It's also why they had to use the pattern "shit(ty|tiest)" instead of just "shit".
You're right, I missed the \b's. Thanks for the correction.
It's all regex anyways
it's like a faster than light spaceship company using horses. There's been infinite solutions to do this better even CPU only for years lol.
hmm not a terrible idea (I think).

You have a semi expensive process. But you want to keep particular known context out. So a quick and dirty search just in front of the expensive process. So instead of 'figure sentiment (20seconds)'. You have 'quick check sentiment (<1sec)' then do the 'figure sentiment v2 (5seconds)'. Now if it is just pure regex then your analogy would hold up just fine.

I could see me totally making a design choice like that.

It's almost as if LLMs are unreliable
I don't know about avoided, this kind of represents the WTF per minute code quality measurement. When I write WTF as a response to Claude, I would actually love if an Antrhopic engineer would take a look at what mess Claude has created.
WTF per minute strongly correlates to an increased token spending.

It may be decided at Anthropic at some moment to increase wtf/min metric, not decrease.

It also increases the number of former customers.
This leak just contributed to a new former customer, me. Flagging these phrases may explain exactly why I noticed cc almost immediatly change into grok lvl shit and never recover. Seriously wtf. (flagged again lol)
/feedback works for that i believe
We used this in 2011 at the startup I worked for. 20 positive and 20 negative words was good enough to sell Twitter "sentiment analysis" to companies like Apple, Bentley, etc...
Did you also forget to ignore case sensitivity back then?
the string is lowercased before the regex is run, fwiw
Smart, that way they will never know when you're shouting at them.
Everyone is commenting how this regex is actually a master optimization move by Anthropic

When in reality this is just what their LLM coding agent came up with when some engineer told it to "log user frustration"

>Everyone is commenting how this regex is actually a master optimization move by Anthropic

No? I'd say not even 50% of the comments are positive right now.

Could you share the regex you used to come up with that sentiment analysis?
(yes|no|maybe)
I'm clearly way too polite to Claude.

Also:

  // Match "continue" only if it's the entire prompt
  if (lowerInput === 'continue') {
    return true
  }
When it runs into an error, I sometimes tell it "Continue", but sometimes I give it some extra information. Or I put a period behind it. That clearly doesn't give the same behaviour.
I always type "please continue". I guess being polite is not a good idea.
Always seems strange to me that people say "please" and "thank you" to LLMs.
It seems strange to you? It's natural to how I write - intentionally avoiding politeness would be weirder to me.

But aside from that, an LLM is only a roleplayer. Treat it like an idiot that makes mistakes and it will act like one. Treat it like a coworker who you respect and it will act like one, and it will find better results.

Obviously nothing about how they act is set in stone but as a general rule this seems to me to be both wise and, in my experience, true as well.

I think if you treat it like a coworker who you respect, it will speak to you like a coworker who respects you, but will still make some idiotic mistakes...
It actually works really well if you suck up to the AI.

"Please do x"

"Thank you, that works great! Please do y now."

"You're so smart!"

lol. It really works though! At least in my experience, Claude gets almost hostile or "annoyed" when I'm not nice enough to it. And I swear it purposefully acts like a "malicious genie" when I'm not nice enough. "It works, exactly like you requested, but what you requested is stupid. Let me show you how stupid you are."

But, when I'm nice, it is way more open, like "Are you sure you really want to do X? You probably want X+Y."

What really works? Sycophancy? I think that is a bug, not a feature.
The only time that function is used in the code is to log it.

    logEvent('tengu_input_prompt', { isNegative, isKeepGoing })
“Go on” works fine too
Makes me wonder what happens once flagged behind the api.
If this code is real and complete then there are no callers of those methods other than a logger line
Nice, "wtaf" doesn't match so I think I'm out of the dog house when the clanker hits AGI (probably).
I guess using French words is safe for now.
Curiously "clanker" is not on the list
That's undoubtedly to detect frustration signals, a useful metric/signal for UX. The UI equivalent is the user shaking their mouse around or clicking really fast.
They also have a "keep going" keyword, literally just "continue" or "keep going", just for logging.

I've been using "resume" this whole time

Continue?
I was thinking the opposite. Using those words might be the best way to provide feedback that actually gets considered.

I've been wondering if all of these companies have some system for flagging upset responses. Those cases seem like they are far more likely than average to point to weaknesses in the model and/or potentially dangerous situations.

That looks a bit bare minimum, not the use of regex but rather that it's a single line with a few dozen words. You'd think they'd have a more comprehensive list somewhere and assemble or iterate the regex checks as needed.
everyone here is commenting how odd it looks to use a regexp for sentiment analysis, but it depends what they're trying to do.

It could be used as a feedback when they do A/B test and they can compare which version of the model is getting more insult than the other. It doesn't matter if the list is exhaustive or even sane, what matters is how you compare it to the other.

Perfect? no. Good and cheap indicator? maybe.

Interesting that expletives and words that are more benign like "frustrating" are all classified the same.
I doubt they're all classified the same. I'd guess they're using this regex as a litmus test to check if something should be submitted at all, they can then do deeper analysis offline after the fact.
Glad abusing words in my list are not in that. but its surprising that they use regex for sentiments.
There is no „stupid” I often write „(this is stupid|are you stupid) fix this”.

And Claude was having in chain of though „user is frustrated” and I wrote to it I am not frustrated just testing prompt optimization where acting like one is frustrated should yield better results.

George Carlin would be very pleased. They missed quite a few of the heavy seven though.
oh I hope they really are paying attention. Even though I'm 100% aware that claude is a clanker, sometimes it just exhibits the most bizarre behavior that it triggers my lizard brain to react to it. That experience troubles me so much that I've mostly stopped using claude code. Claude won't even semi-reliably follow its own policies, sometimes even immediately after you confirm it knows about them.
Probably a lot of my prompts have been logged then. I’ve used wtf so many times I’ve lost track. But I guess Claude hasn’t
Did you notice a change in quality after you went foul?
I find when you give harsh feedback to claude it becomes "neurotic" and worthless, if "wtf" enters the chat, then you know it's time to restart or DIY.
Not really. Most of the times it actually finally picks up on what I was telling it to do. Sometimes it takes a few tries, like 2-3 wtfs. I don’t think I’ve ever given it more than 3 consecutive wtfs, and that would be a lot

It’s about a once a week or less event. A bit annoying sometimes, but not a deal breaker

Hmm.. I flag things as 'broken' often and I've been asked to rate my sessions almost daily. Now I see why.
i dislike LLMs going down that road, i don't want to be punished for being mean to the clanker
OMG WTF
Surely "so frustrating" isn't explicit content?
If anyone at anthropic is reading this and wants more logs from me add jfc.
wondering how this fares for languages other than English.
so they think that everybody on earth swears only in english?
you'd better be careful wth your typos, as well
> terrible

I know I used this word two days ago when I went through three rounds of an agent telling me that it fixed three things without actually changing them.

I think starting a new session and telling it that the previous agent's work / state was terrible (so explain what happened) is pretty unremarkable. It's certainly not saying "fuck you". I think this is a little silly.

Yeah, this is crazy
so frustrating..
i wish that's for their logging/alert. i definitely gauge model's performance by how much those words i type when i'm frustrated in driving claude code.
Ridiculous string comparisons on long chains of logic are a hallmark of vibe-coding.
It's actually pretty common for old sysadmin code too..

You could always tell when a sysadmin started hacking up some software by the if-else nesting chains.

Nah, it's a hallmark of your average codebase in pre-LLM era.